[Flutter跨平台]五、Dart核心2-类

21 阅读2分钟

1. 类

后面学习Flutter的时候,里面每个组件就是一个类;

1.1. 定义类

void main() {
  Person p = new Person();
  // Person p = Person(); //这样不写new也行
  p.name = '张三';
  p.age = 19;
  print(p.name);
  print(p.age);
  p.eat();
}

class Person {
  String? name;
  int? age;

  eat() {
    print('$name在吃饭');
  }
}

1.2. 默认构造函数

// 无参数的构造函数,就是默认的构造函数
void main() {
  Person p = new Person();
  // print(p);
  print(p.name);
  print(p.age);
}

class Person {
  String? name;
  int? age;

  //这就是默认的构造函数,当new出来这个类时,它将自动运行;
  Person() {
    print('我是构造函数');
  }

  eat() {
    print('$name在吃饭');
  }
}

1.3. 有参数的构造函数

// 有参数的构造函数: 自定义构造函数
// 自定义与类同名的构造函数时,可以有参数
// 注意点:
// - 与类同名的构造函数只能有一个
// - 如果自定义了该类名构造函数,那么默认的构造函数就失效
void main() {
  Person p = new Person('张三', 19);
  print(p.name);
  print(p.age);
}

class Person {
  String? name;
  int? age;

  // 1. 自定义构造函数:有参数的构造函数
  // Person(String name, int age) {
  //   this.name = name;
  //   this.age = age;
  // }

  // 2. 简写
  Person(this.name, this.age);

  eat() {
    print('$name在吃饭');
  }
}

1.4. 命名构造函数

// 命名构造函数:
// 可以给构造函数命名;
// Dart的命名构造函数允许一个类有多个不同的构造方法。这在需要用不同的数据或方式创建同一个类的对象时特别有用。(个人感觉有点类似多态)
// 比如,一个类可以有一个默认构造函数和一个fromJson命名构造函数,用于从JSON数据创建对象
void main() {
  Person p = new Person('张三', 19);
  print({p.name, p.age});

  Person p1 = new Person.withInfo('李四', 25);
  print({p1.name, p1.age});

  Person p2 = new Person.withInfo('王五', 29);
  print({p2.name, p2.age});
}

class Person {
  String? name;
  int? age;

  // 自定义构造函数:有参数的构造函数
  // Person(String name, int age) {
  //   this.name = name;
  //   this.age = age;
  // }
  // 简写
  Person(this.name, this.age);

  // 命名构造函数
  // Person.withInfo(String name, int age) {
  //   this.name = name;
  //   this.age = age;
  // }
  // 命名构造函数 简写
  Person.withInfo(this.name, this.age);

  //另外还能再写
  Person.aa(this.name, this.age);

  eat() {
    print('$name在吃饭');
  }
}

1.5. 私有属性和方法

// 就是在属性和方法前面加一条下划线“_”,这样就只有在类里面才能使用这个加了下划线的属性/方法了
import 'lib/Dog.dart';

void main() {
  Dog p = new Dog('小狗a', 16);
  print({p.name, p.age});
  p.eat('骨头');
  // print(p.aaa);
  // p.printaaa();
}

1.6. 类的继承

void main() {
  Cat p = new Cat('小猫a', 10);
  print({p.name});
  p.sound();

  Dog d = new Dog('小狗q');
  d.sound();
}

// 父类
class Animal {
  String? name;

  // 简写
  Animal(this.name);
}

// 子类1
class Cat extends Animal {
  int? weight;

  Cat(super.name, this.weight);
  sound() {
    print('喵喵喵');
  }
}

// 子类2
class Dog extends Animal {
  Dog(super.name);

  sound() {
    print('汪汪汪');
  }
}

1.7. 类的继承-重写

void main() {
  // 实例化男人对象
  Man p = new Man('张三', 19);
  p.eat();

  // 实例化女人对象
  Woman w = new Woman('小芳', 21);
  w.eat();
}

class Person {
  String? name;
  int? age;

  // 简写
  Person(this.name, this.age);

  eat() {
    print('$name在吃饭');
  }
}

// 男人类
class Man extends Person {
  Man(super.name, super.age);

  //重写父类中的方法
  @override
  eat() {
    print('$name喜欢吃很多碗饭');
  }
}

// 女人类
class Woman extends Person {
  Woman(super.name, super.age);
}

1.8. Mixin扩展

void main() {
  // 实例化男人对象
  Man m = new Man('张三', 19);
  m.eat();
  m.sing('张三');
  print(m.aa);

  // 实例化女人对象
  // Woman w = new Woman('小芳', 21);
  // w.eat();
  // w.dance();
}

class Person {
  String? name;
  int? age;

  // 简写
  Person(this.name, this.age);

  eat() {
    print('$name在吃饭');
  }
}

// 男人类,使用with来连接mixin类
class Man extends Person with SingMixin {
  Man(super.name, super.age);

  //重写父类中的方法
  @override
  eat() {
    print('$name喜欢吃很多碗饭');
  }
}

// 女人类
class Woman extends Person with DanceXixin {
  Woman(super.name, super.age);
}

// mixin类
mixin SingMixin {
  String aa = '这是mixin中的属性';

  sing(String name) {
    print('$name可以唱歌...');
  }
}

mixin DanceXixin {
  dance() {
    print('我可以跳舞');
  }
}

1.9. 封装、多态、继承

  • 就是封装;它只向外提供可访问的属性或接口(也就是方法);
  • 多态:有两种实现方式
    • 编译时多态:重载来实现:JAVA支持传统的重载(就是类中写多个名字一样 但参数不一样的多个方法,调用时入几个参数就自动选哪个方法),Dark不支持传统的重载,JS可以以其他的方式实现(支持方法重载,通过签名声明实现,实际编译是单一函数);
    • 运行时多态:重写来实现:通过继承类/实现接口来实现,后代分支自己独特的方法,就用@override重写来写自己独特的方法 这就是多态
  • 继承:就是子类继承父类;子类自动可使用父类的属性及方法;子类的 构造函数得写 super.属性 才行;