Flutter Dart 类

24 阅读1分钟

普通构造函数 默认有无参构造

class Point {
  int x;
  int _y;
  Point(this.x, this._y);
}

命名构造函数

  //重定向命名构造函数 = kotlin次构造调用主构造
  Point.fromJson(Map map) : this(map['x'], map['y']);

初始化列表构造函数

  Point.fromJson2(int x, int y)
      : x = x,
        _y = y;

常量构造函数(提高渲染速度)

//常量构造函数 const + final 关键字
class ImmutablePoint {
  final double x, y;

  const ImmutablePoint(this.x, this.y);
}

访问权限

同一个文件里默认都可以访问

如果不同的文件则需要在变量前加_ 来设置私有

  Point.fromJson2(int x, int y)
      : x = x,
        _y = y;

例子

void main(List<String> args) {
  //构造函数
  var point = Point(20, 30);
  print(point._y);

  var point2 = Point.fromJson({'x': 1, 'y': 2});
  print(point2._y);

  var point3 = Point.fromJson2(3, 4);
  print(point3._y);

  var ip = ImmutablePoint(11, 22);
  print(ip.y);

  //获取对象的类型
  print('runtimeType = ${ip.x.runtimeType}');
}

//常量构造函数 const + final 关键字
class ImmutablePoint {
  final double x, y;

  const ImmutablePoint(this.x, this.y);
}

//普通构造函数 默认有无参构造
class Point {
  int x;
  int _y;
  Point(this.x, this._y);

  // Point(int x,int y){
  //   this.x = x;
  //   this._y = y;
  // }
  //重定向命名构造函数 = kotlin次构造调用主构造
  Point.fromJson(Map map) : this(map['x'], map['y']);

  //初始化列表构造函数
  Point.fromJson2(int x, int y)
      : x = x,
        _y = y;
}

enum Color {
  Red,
  Yellow,
  Blue,
  Green;
}

抽象类

继承 和 java 用法类似

void main(List<String> args) {
  Doer doer = EffectiveDoer();
  doer.doSomething();

  final creator = AbstractCreator.create2("ConcreteCreator2");
}

abstract class Doer {
  void doSomething();
  void rest();
}

class EffectiveDoer extends Doer {
  @override
  void doSomething() {
    print("EffectiveDoer doSomething");
  }

  @override
  void rest() {}
}

abstract class AbstractCreator<T> {
  T create();

  AbstractCreator();

  factory AbstractCreator.create2(String str) {
    if (str == "ConcreteCreator") {
      return ConcreteCreator();
    } else {
      return ConcreteCreator2();
    }
  }
}

class ConcreteCreator<T> extends AbstractCreator<T> {
  @override
  create() {
    // TODO: implement create
    throw UnimplementedError();
  }
}

class ConcreteCreator2<T> extends AbstractCreator<T> {
  @override
  create() {
    // TODO: implement create
    throw UnimplementedError();
  }
}

class Greet{
  void greet(){

  }
}

class Person implements Greet{
  
  @override
  void greet() {
    // TODO: implement greet
  }
}
    

mixin 混入

void main(List<String> args) {
  var p = Processor();
  p.process();
  Musician().play();
}

class Musician extends Performer with Musical {
  void play() {
    print("canPlayPiano ${canPlayPiano} canPlayGuiter ${canPlayGuater}");
  }
}

class Performer {}

mixin Musical {
  bool canPlayPiano = false;
  bool canPlayGuater = true;
}

class Processor extends Taggable with Logger {
  void process() {
    log("x");
  }
}

abstract class Taggable{
  String tag = "my_tag";
}

mixin Logger on Taggable{
  void log(Object msg) {
    print("[$tag] $msg");
  }
}

枚举

values 是数组类型

enum Color { red, green, blue }

void main() {
  //通过index 返回枚举中具体常量的值
  print(Color.green.index);

  //通过 values List 数组返回常量列表
  print(Color.values);
}