美文网首页
Flutter系列之Dart函数、类与运算符

Flutter系列之Dart函数、类与运算符

作者: 過愙2015 | 来源:发表于2019-08-19 17:52 被阅读0次

编程语言虽然千差万别,但归根结底,设计思想无非是表示信息与处理信息

在Flutter系列之Dart语言概述中已经介绍了Dart如何表示信息,本篇将介绍Dart是如何处理信息的

作为一门真正面向对象的编程语言,Dart将处理信息的过程抽象为了对象,而函数、类与运算符则是抽象中最重要的手段

函数

函数是一段用来独立完成某个功能的代码片段,而Dart中所有类型都是对象类型,函数也不例外,即函数也是对象,它的类型为Function。

void main() {

  Function check = isEmptyStr;

  printStr('',check);

  printStr('Hello World!',check);

}

bool isEmptyStr(String str){//判断字符串是否为null

  return str.isEmpty;

}

void printStr(String str,Function check){//用check函数来判断String是否为null

  if (check(str)) {

    print('$str is null');

  }else {

    print('$str is not null');

  }

}

is null

Hello World! is not null

上面代码中,首先定义了一个判断字符串是否为null的函数isEmptyStr,并把它传给了另一个打印输出的函数printStr,即函数也可以被定义为变量,甚至可以作为参数传递给其他函数

日常开发中,经常会遇到一个函数中需要传递多个参数的情况,这种情况下Dart与其他语言如c、c++、java等的做法是不同的

c、c++、java等的做法是提供函数的重载,即函数同名但参数不同,而Dart认为重载会导致混乱,因此不支持重载,而是提供了可选参数和可选命名参数

具体方式为,在申明函数时:

·给参数添加{},以paramName:value的方式指定调用参数,即可选命名参数

·给参数加[],则意味着这些参数可以忽略,即可选参数

不仅如此,在使用这两种方式定义函数时,还可以给参数设置默认值

注意:可选参数和可选命名参数不可一起使用,可选参数和可选命名参数在申明时需放在其他参数后面,且可选命名参数调用时与申明的顺序无关,但可选参数调用时与申明的顺序有关

void main() {

  _ptionalNamedParameter('Tom',2,sex:'男');

  _ptionalNamedParameter('Tom',2,sex:'男',age:5);

  _ptionalNamedParameter('Tom',2);

  _ptionalNamedParameter('Tom',2,age:25);

  _ptionalNamedParameter('Tom',2,age:18,sex:'女');

  _ptionalParameter('Tom',2,'男');

  _ptionalParameter('Tom',2,'男',5);

  _ptionalParameter('Tom',2);

  //_ptionalParameter('Tom',2,25);//错误

  //_ptionalParameter('Tom',2,18,'女');//错误

}

bool isNotEmptyStr(String str) {

  return str.isNotEmpty;

}

void printStr(String str,Function check){//用check函数来判断String是否为null

  if (check(str)) {

    print('$str is null');

  }else {

    print('$str is not null');

  }

}

//可选命名参数:在需要定义为可选命名参数时给参数加{},并且可以给可选命名参数设置默认值,必须定义在其他参数后面,参数顺序可变,不可与可选参数同用

void _ptionalNamedParameter(String name, int classes, {String sex, int age = 18}) {

  print('_ptionalNamedParameter:name is $name,classes = $classes,sex is $sex,age = $age');

}

//可选参数:在需要定义为可选参数时给参数加[],并且可以给可选参数设置默认值,必须定义在其他参数后面,参数顺序不可变,不可与可选命名参数同用

void _ptionalParameter(String name, int classes, [String sex = '男', int age]) {

  print('_ptionalParameter:name is $name,classes = $classes,sex is $sex,age = $age');

}

_ptionalNamedParameter:name is Tom,classes = 2,sex is 男,age = 18

_ptionalNamedParameter:name is Tom,classes = 2,sex is 男,age = 5

_ptionalNamedParameter:name is Tom,classes = 2,sex is null,age = 18

_ptionalNamedParameter:name is Tom,classes = 2,sex is null,age = 25

_ptionalNamedParameter:name is Tom,classes = 2,sex is 女,age = 18

_ptionalParameter:name is Tom,classes = 2,sex is 男,age = null

_ptionalParameter:name is Tom,classes = 2,sex is 男,age = 5

_ptionalParameter:name is Tom,classes = 2,sex is 男,age = null

类是特定类型的数据和方法的集合,也是创建对象的模板

类的定义及初始化

Dart是面向对象的语言,每个对象都是一个类的实例,都继承自顶层类型Object

注意:Dart中并没有public、protected、private等关键字,取而代之的是只要在申明变量和方法时,在其前面加上“”即可作为private使用,如果不加“”,则默认为public。不过,“_”的限制范围并不是类访问级别的,而是库(package)访问级别的

void main() {

Student student = new Student('Jack', '男', age:18);

  student.printStudentInfo();

  var s=Student('Tom', '男');//省略new关键字

  s.printStudentInfo();

  Student.studentId = 1;

  Student.printStudentId();

}

class Student {

  String name;

  String sex;

  int age;

  static int studentId = 0;

  //语法糖,相当于在函数体内:this.name=name;this.sex=sex;this.age=age;

  Student(this.name, this.sex, {this.age});

  void printStudentInfo() => print('name is $name, sex is $sex, age is $age');

  static void printStudentId() => print('studentId is $studentId');

}

name is Jack, sex is 男, age is 18

name is Tom, sex is 男, age is null

studentId is 1

上面示例中,定义了三个成员变量,并通过构造函数语法糖对三个成员变量进行初始化操作;定义了一个静态成员变量,并在初始化时赋值默认值,并定义了两个方法分别打印三个成员变量及一个静态成员变量的值

有的时候类的实例化需要根据参数提供多种初始化方式。除了可选命名参数和可选参数之外,Dart还提供了命名构造函数的方式,使得实例化过程语义更清晰

注意:Dart支持初始化列表。在构造函数真正执行之前,可以先给实例变量赋值,甚至可以重定向之另一个构造函数

void main() {

  Student student = new Student.nameInfo('Tom');

  student.printStudentInfo();

}

class Student {

  String name;

  String sex;

  int age;

  Student(this.name, this.sex) : age = 18; //初始化变量age

  Student.nameInfo(String name) : this(name, '男'); //重定向构造函数

  void printStudentInfo() => print('name is $name, sex is $sex, age is $age');

}

name is Tom, sex is 男, age is 18

上面示例中,Student中有两个构造函数:Student和Studnet.nameInfo,其中,Studnet.nameInfo将成员变量的初始化重定向到了Student中,而Student则在初始化列表中为age赋值默认值

复用

在面向对象的编程语言中,将其他类的变量与方法纳入本类中进行复用的方式一般有两种:继承和接口,而Dart也是面向对象的编程语言,因此,在Dart中,复用的方式也是继承和接口

继承:子类由父类派生,会自动获取父类的成员变量和方法实现,子类可以根据需要覆写构造函数及父类方法

接口:子类获取到的仅仅是接口的成员变量符号和方法符号,需要重新实现成员变量,以及方法的申明和初始化,否则编译器会报错

以下示例会对继承和接口的区别进行说明

void main() {

Android android = new Android();

  android

    ..brandName = '华为'

    ..phoneModel = 'P30 Pro'

    ..edition = 'EMUI9.1.1';//级联运算符,相当于android.brandName = '华为';android.phoneModel = 'P30 Pro';android.edition = 'EMUI9.1.1';

  android.printInfo();

  IOS ios = new IOS();

  ios

    ..phoneModel = 'iPhone XR'

    ..edition = 'iOS 12';//级联运算符,相当于phone.phoneModel = 'iPhone XR';phone.edition = 'iOS 12';

  ios.printInfo();

}

class Phone {

  String phoneModel; //手机型号

  String edition; //手机版本

  void printInfo() =>

      print('This Phone phoneModel is $phoneModel, and edition is $edition');

}

//Android继承自Phone

class Android extends Phone {

  String brandName; //品牌

  @override

  void printInfo() {

    //覆写了printInfo实现

    print('This Phone brandName is $brandName, phoneModel is $phoneModel, and edition is $edition');

  }

}

//IOS实现了Phone接口

class IOS implements Phone {

  //成员变量需要重新申明

  String phoneModel;

  String edition;

  //方法需要重新实现及初始化

  void printInfo() => print('This is IOS Phone, phoneModel is $phoneModel, and edition is $edition');

}

This Phone brandName is 华为, phoneModel is P30 Pro, and edition is EMUI9.1.1

This is IOS Phone, phoneModel is iPhone XR, and edition is iOS 12

以上代码为Android通过继承Phone的方式添加了成员变量,并覆写了printInfo()的实现;IOS通过接口实现的方式,覆写了Phone的变量定义及方法实现;而从以上代码也能看出,接口并没有为我们带来实际好处

注意:Dart和其他编程语言一样,也不支持多继承

其实,Dart除了继承和接口外,还给我们提供了另一种机制来实现类的复用,即“混入”(Mixin):混入鼓励代码重用,可以认为是具有实现方法的接口

要使用混入,只需要with关键字即可

void main() {

  IOS ios = new IOS();

  ios

    ..phoneModel = 'iPhone XR'

    ..edition = 'iOS 12';//级联运算符,相当于phone.phoneModel = 'iPhone XR';phone.edition = 'iOS 12';

  ios.printInfo();

}

class Phone {

  String phoneModel; //手机型号

  String edition; //手机版本

  void printInfo() =>

      print('This Phone phoneModel is $phoneModel, and edition is $edition');

}

class IOS with Phone {}

This Phone phoneModel is iPhone XR, and edition is iOS 12

如上示例可知:通过混入,一个类里可以以非继承的方式使用其他类中的变量和方法,即通过混入,可以解决单继承问题

运算符

Dart除了和其他大部分编程语言的运算符一样外,还提供了几个额外的运算符,用于简化处理变量实例缺失(null)的情况

?.运算符:假设Phone类中有个printInfo()方法,phone是Phone类的一个可能为null的实例,phone调用成员方法的安全方法,可以简化为phone.?printInfo(),表示如果phone为null,则跳过printInfo()方法

??=运算符:如果变量a为null,则给a赋值value,负责跳过。a??=value

??运算符:如果a不为null,返回a的值,否则返回b的值。a??b

void main() {

  Phone phone;

  phone?.printInfo();//由于phone为null,因此不会执行printInfo()方法

  Test();

}

class Phone {

  String phoneModel; //手机型号

  String edition; //手机版本

  void printInfo() =>

      print('This Phone phoneModel is $phoneModel, and edition is $edition');

}

void Test(){

  int age;

  print('age=$age');

  print('age=${age??16}');//??运算符,age为null,返回16

  age??=18;//??=运算符,age为null,给age赋值为18

  print('age=$age');

  print('age=${age??20}');//??运算符,age不为null,返回18

  age??=22;//??=运算符,age不为null,返回18

  print('age=$age');

}

age=null

age=16

age=18

age=18

age=18

在Dart中,一切都是对象,连运算符也是对象成员方法的一部分

对于系统的运算符,一般情况下只支持基本数据类型和标准库中提供的类型。而对于用户自定义的类,如果想支持基本操作,比如比较大小、相加相减等,则需要用户自己来定义关于这个运算符的具体实现

Dart提供了运算符的覆写机制,我们不仅可以覆写方法,还可以覆写或者自定义运算符

operator是Dart的关键字,与运算符一起使用,表示一个类成员运算符方法

void main() {

  Vector v = new Vector(5, 3);

  Vector v2 = new Vector(1, 2);

  Vector v3 = new Vector(4, 1);

  Vector v4 = v - v2;

  print('x = ${v4.x}, y = ${v4.y}');

  print(v3 == (v - v2));

}

class Vector {

  num x, y;

  Vector(this.x, this.y);

  //自定义相减运算符

  Vector operator -(Vector v) {

    return Vector(x - v.x, y - v.y);

  }

  //覆写相等运算符

  bool operator ==(dynamic v) {

    return (x == v.x && y == v.y);

  }

}

x = 4, y = 1

true

文章已同步更新至微信公众号,欢迎关注

相关文章

  • Flutter系列之Dart函数、类与运算符

    编程语言虽然千差万别,但归根结底,设计思想无非是表示信息与处理信息 在Flutter系列之Dart语言概述中已经介...

  • 2019-09-19: 七:Flutter之Dart第四节(运算

    七:Flutter之Dart第四节(运算符)? 7: 运算符这里我列举出相对比较特殊的运算符、你可能会疑惑Dart...

  • flutter系列

    flutter系列--Dart语法

  • Flutter之基础Widget

    原文博客地址: Flutter之基础Widget Flutter和Dart系列文章 项目GitHub地址 Flut...

  • dart:函数、类与运算符

    编程语言虽然千差万别,但归根结底,它们的设计思想无非就是回答两个问题:如何表示信息和如何处理信息。作为一门真正面向...

  • 2019-09-19: 六:Flutter之Dart第三节(函数

    六:Flutter之Dart第三节(函数)? 6.1: 函数的基本定义Dart是一种真正的面向对象语言、所以即使函...

  • Dart 基础

    为什么学习dart?谷歌推出了flutter,而flutter是基于dart语言的 dart是函数式编程语言 所有...

  • flutter Dart语法

    Dart语言特点: 打印语法: 一、变量与常量 二、数据类型 三、函数 四、运算符 五、控制流程 六、类 七、泛型...

  • 【Flutter】Dart的方法与箭头函数

    ?【Flutter】学习养成记,【程序员必备小知识】 ? 今日小知识——【Flutter】Dart的方法与箭头函数...

  • Flutter之容器类Widget

    Flutter和Dart系列文章 项目GitHub地址 容器类Widget和布局类Widget都作用于其子Widg...

网友评论

      本文标题:Flutter系列之Dart函数、类与运算符

      本文链接:https://www.haomeiwen.com/subject/qfuesctx.html