Flutter之Dart语法(二)面向对象

在Dart语法中,用关键字class表示类。

class Person {
  
}
  • 私有和公有

Dart中公有和私有的界定是以文件界定的,并不是通过类界定的,如果不允许其他文件访问当前文件中的成员变量,或者不允许调用方法,可以定义成员变量或者方法的时候以下划线开始。

class Person {
  //内部变量
  String _id = "111";
  //外部变量
  String? name;
  //内部方法
  void _printId() => print("id is $_id");
  //外部方法
  void printName() => print("name is $name");
}

注意,如果是在其他文件中定义一个Person子类,是不能继承内部方法的。如果是在本文件中是可以继承的。

//本文件中是没问题的,编译器是有提示的。
class Teacher extends Person {
  Teacher(String id, int age) : super(id, age);

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

}
//其他文件中只能继承外部方法
class Student extends Person {
  Student.initWith(String name, int age) : super.initWith(name, age);

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

}
  • 构造方法
class Person {
  String? name;
  String _id = "111";
  late int age;

  //默认构造函数
  // Person(this._id, this.age);

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

  //带有初始化列表的构造函数
  Person(String id,int age):assert(age > 0) {
    this.age = age;
    this._id = id;
  }

}
  • 类属性和类方法

Dart中类变量和类方法都用static关键字修饰,在调用的时候方法如下。

void _test() {
  Person.isa = "1111111111";
  Person.getMethList()
}

class Person {
  String? name;
  String _id = "111";
  late int age;

  static String isa = "0";

  static void getMethList() {
    print("it is not object-c");
  }
}
  • 单例

Dart中,构造函数是没有return的,如果想要ruturn,需要在构造函数前面加上关键字factory,原始写法

class Application {
  static  Application? _shared;

  // 私有的命名构造函数,外界不能调用
  Application._init();

  // 使用factory关键字
  factory Application() {
    if (_shared == null) {
      _shared = Application._init();
    }
    return _shared!;
  }

}

可以稍作优化,三行代码搞定单例。

class Application {
  static  Application? _shared;

  // 私有的命名构造函数,外界不能调用
  Application._init();

  factory Application() => _shared ??= Application._init();
}
  • 继承

Dart中是单继承,继承一个类后,可以通过@override来重写父类中的方法,也可以通过super来调用父类中的方法。构造函数是不能呗继承的。

class Animal extends Object {
  late double  height;
  late double  weight;

  void eat() => print("eat food"); 
  void sleep() => print("sleep");
}

class Dog extends Animal {
  @override
  void eat() => print("eat shit");
}

class Cat extends Animal {

}
  • 接口(implements)

Dart中没有interface关键字,但每个类都是隐式的接口。当类被abstract修饰的时候(抽象类),是无法被实例化为对象的,只能当接口。

void _test() {
    //这里会报错
    CanAccompanyMaster acc = new CanAccompanyMaster();
  }

//接口定义
abstract class CanAccompanyMaster {
  void dance() {
    print("dance");
  }

  void makeFun();
  
}

class MakeFood {
  void cook() {
    print("as delicious food");
  }
}

//Dart中每个类都是一个隐式的接口
//接口中的方法需要被重新实现
class Pig extends Animal implements CanAccompanyMaster, MakeFood {
  @override
  void cook() {
    // TODO: implement cook
  }

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

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

}
  • 混入(mixins)

在面向对象的语音中,mixins是一个可以把自己的方法提供给其他类使用,并且可以不需要成为其他类子类的类。以非继承的方式来复用类中的代码。要使用mixins,用关键字with来复用类中的代码。


class Cat extends Animal {
  void catchMouse() => print("catch mouse");
}

// The class 'Cat' can't be used as a mixin because it extends a class other than Object
// class Dog extends Animal with Cat {
//   @override
//   void eat() => print("eat shit");
// }

class LitterCat {
  void catchMouse() => print("catch mouse");
}

class Dog extends Animal with LitterCat {
  @override
  void catchMouse() {
    // TODO: implement catchMouse
    super.catchMouse();
  }
}

注意,如果是作为混合的类,不能有extends。如有有extends编译器会报错。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值