Dart入门05 -- 私有成员,静态成员,继承,抽象类,隐式接

2022-02-19  本文已影响0人  YanZi_33

私有成员

class Person {
  String name;
  int age;
  //私有成员
  double _weight;

  //其中age为可选参数
  Person.init(this.name, this.age, this._weight);

  //使用set,get关键字的setter,getter方法
  set setName(String name) {
    this.name = name;
  }

  String get getName {
    return this.name;
  }

  double get getWeight {
    return this._weight;
  }
}
import 'person.dart';

void main(List<String> args) {
  Person person1 = Person.init("cccc", 26, 125.5);
  //_weight 是私有成员 不能直接访问
  print(person1.getWeight);
}

静态成员

void main(List<String> args) {
  Car car = new Car("bsj", 12500.0);
  //静态属性
  Car.speed = 125.6;
  //实例方法 调用者为实例对象
  car.run();
  //静态方法 调用者为类名
  Car.walk();
}

class Car {
  String name;
  static double speed;
  double weight;

  Car(this.name, this.weight);

  void run() {
    print("run");
  }

  static void walk() {
    print("walk");
  }
}

类的继承

main(List<String> args) {}

class Animal {
  int age;

  Animal(this.age);
}

class Person extends Animal {
  String name;
  
  //必须完成父类属性age的初始化 在初始化列表中完成
  Person(this.name, int age) : super(age);
}

抽象类

void main(List<String> args) {
  Circle circle = new Circle();
  circle.draw();
  circle.show();
}

abstract class Shap {
  void draw();
  void show() {
    print("show");
  }
}

//Circle必须实现draw 抽象方法 否则会报错
class Circle extends Shap {
  @override
  void draw() {
    print("绘制圆形");
  }
}
main(List<String> args) {
  //抽象类不能实例化
  final s = Shape();

  //Map是系统的一个抽象类
  //Map能实例化 是因为Map内部实现了一个工厂构造函数 external factory Map()
  final map = Map();
  print(map.runtimeType);
}

//Shape是一个抽象类
abstract class Shape {
  //getArea是抽象方法 没有实现体的 由子类去实现
  void getArea();
}

//继承抽象类的子类 必须实现抽象类中定义的抽象方法
class Rectanle extends Shape {
  @override
  void getArea() {
    print("画矩形");
  }
}

隐式接口

main(List<String> args) {
  
}

class Animal {
  void eat() {
    print("eat");
  }
}

class Runnner {
  void run() {
    print("run");
  }
}

class Flyer {
  void fly() {
    print("fly");
  }
}

//当将一个类当作接口使用时,那么实现这个接口的类,必须实现这个接口中的所有方法
class Superman extends Animal implements Runnner, Flyer {

  @override
  void eat() {
    // TODO: implement eat
    super.eat();
  }

  @override
  void run() {
    // TODO: implement run
  }

  @override
  void fly() {
    // TODO: implement fly
  }
}

混入mixin

void main(List<String> args) {
  
}

abstract class Runner {
  void run();
}

abstract class Flyer {
  void fly();
}

class Animal {
  void eat() {
    print("Animal eat");
  }
}

//SuperMan实现接口类Runner与Flyer
//必须实现Runner与Flyer中所有方法,否则会报错
class SuperMan extends Animal implements Runner, Flyer {
  @override
  void run() {
    // TODO: implement run
  }

  @override
  void fly() {
    // TODO: implement fly
  }
}
void main(List<String> args) {}

mixin Runner {
  void run() {}
}

mixin Flyer {
  void fly() {}
}

class Animal {
  void eat() {
    print("Animal eat");
  }
}

//SuperMan with 混入类Runner与Flyer
//可有选择的实现Runner与Flyer中的方法
class SuperMan extends Animal with Runner, Flyer {
  @override
  void run() {
    // TODO: implement run
    super.run();
  }
}
上一篇 下一篇

猜你喜欢

热点阅读