zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

【Flutter】Dart 面向对象 ( 命名构造方法 | 工厂构造方法 | 命名工厂构造方法 )

flutter 命名 面向对象 工厂 Dart 构造方法
2023-06-13 09:17:49 时间

文章目录

一、 命名构造方法


命名构造方法 :

  • 定义格式 : 类名.方法名()
Student.cover(Student student):super(student.name, student.age);
  • 父类构造函数 : 如果父类没有默认构造函数, 子类必须调用父类的构造函数 ;
  • 方法体 : 命名构造方法与普通构造函数一样 , 可以有方法体 , 也可以不写方法体 ;
  // 命名构造方法也可以有方法体
  Student.init(Student student):super(student.name, student.age){
    print("命名构造方法 : name : ${student.name}, age : ${student.age}");
  }

代码示例 :

// 定义 Dart 类
// 与 Java 语言类似, 所有的类默认继承 Object 类
class Person{

  // 定义变量
  String name;
  int age;

  // 标准构造方法, 下面的方法是常用的构造方法写法
  Person(this.name, this.age);

  // 重写父类的方法
  @override
  String toString() {
    return "$name : $age";
  }
}

// 继承
class Student extends Person{

  // 命名构造方法
  // 定义格式 : 类名.方法名()
  // 父类构造函数 : 如果父类没有默认构造函数, 子类必须调用父类的构造函数
  Student.cover(Student student):super(student.name, student.age);

  // 命名构造方法也可以有方法体
  Student.init(Student student):super(student.name, student.age){
    print("命名构造方法 : name : ${student.name}, age : ${student.age}");
  }

}

二、 工厂构造方法


工厂构造方法就是 单例模式 , 工厂构造方法作用是返回之前已经创建的对象 , 之前创建对象时需要缓存下来 ;

工厂构造方法规则 : 在构造方法前添加 factory 关键字 ;

定义了工厂构造方法的类 :

// 使用工厂构造方法实现单例模式
// 工厂构造方法就是单例模式
// 工厂构造方法作用是返回之前已经创建的对象 , 之前创建对象时需要缓存下来 ;
class Student2{
  // 静态成员
  static Student2 instace;

  // 工厂构造方法
  factory Student2(){
    if(instace == null){
      // 调用命名构造方法创建 Student2 对象
      instace = Student2.init();
    }
    // 返回单例对象
    return instace;
  }

  // 命名构造方法
  Student2.init();
}

测试工厂构造方法 :

  factoryConstructorDemo(){
    Student2 stu1 = Student2();
    print("stu1 创建完成 : ${stu1}");
    Student2 stu2 = Student2();
    print("stu2 创建完成 : ${stu2}");

    print("对比 stu1 与 stu2 : stu1 == stu2 : ${stu1 == stu2}");
  }

执行结果 :

I/flutter (32158): stu1 创建完成 : Instance of 'Student2'
I/flutter (32158): stu2 创建完成 : Instance of 'Student2'
I/flutter (32158): 对比 stu1 与 stu2 : stu1 == stu2 : true

三、 命名工厂构造方法


命名工厂构造方法格式 :

factory 类名.方法名

命名工厂构造方法可以有 返回值 ;

如果类中有 final 修饰的成员 , 在命名构造方法中必须对其进行初始化 ;

但是在命名工厂构造方法中 , 可以不初始化 final 类型成员

命名工厂构造方法示例 :

// 继承
class Student extends Person{

  // 私有变量, 以下划线开始的变量是私有变量
  int _grade;

  String school;
  String city;
  String address;

  // 父类构造函数调用 : 如果父类有非空参数的构造函数, 子类必须实现相同参数的构造函数
  // 如果该类有父类 , 那么先调用父类的构造方法 , 完成父类的初始化
  // 然后才能完成自己的初始化
  //
  // this.school 指定自有参数
  // {this.school} 是可选参数, 可选参数必须在构造函数参数列表中最后一个
  //
  // 默认参数 : 可选参数中如果用户不初始化该可选参数 , 那么为其指定一个默认值
  // {this.city = "北京"} 指定了如果用户不初始化 city 变量, 那么为其初始化 "北京" 字符串值
  //
  // 初始化列表 : 冒号后面的内容就是初始化列表
  //            父类构造器也是初始化列表
  //            除了父类构造方法之外 , 还可以在子类构造方法体之前初始化示例变量
  //            不同的初始化实例变量之间使用逗号隔开
  //
  // 父类构造方法 : 如果父类没有默认构造方法 (无参构造方法) ,
  //              必须在初始化列表中调用父类构造函数 , super(name, age) ;
  //
  // 构造方法方法体 : 可以省略 ;
  Student(this._grade, String name, int age,
         {this.school, this.city = "北京"})
         : address = "北京市海淀区" ,
         super(name, age);


  // 命名构造方法
  // 定义格式 : 类名.方法名()
  // 父类构造函数 : 如果父类没有默认构造函数, 子类必须调用父类的构造函数
  Student.cover(Student student):super(student.name, student.age);

  // 命名构造方法也可以有方法体
  Student.init(Student student):super(student.name, student.age){
    print("命名构造方法 : name : ${student.name}, age : ${student.age}");
  }

  // 命名工厂构造方法 : factory 类名.方法名
  // 命名工厂构造方法可以有返回值
  // 如果类中有 final 修饰的成员 , 在命名构造方法中必须对其进行初始化
  //    但是在命名工厂构造方法中 , 可以不初始化 final 类型成员
  factory Student.init2(){
    return Student(1, "Tom", 18);
  }

}

四、 相关资源


参考资料 :

博客源码下载 :