flutterdart语法基础

Dart 重写==实现对象判断

2019-04-20  本文已影响0人  奔跑的杰尼龟zxl
is

is 关键字为类型检查关键字

重写==

下面案例运用了is关键字,检查类型,并以判断属性值是否相等来检查对象是否相等。

class Student{
  String name;
  int age;
  String school;

  Student.create(this.name,this.age,this.school);

  @override
  bool operator ==(other) {
    // 判断是否是非
    if(other is! Student){
      return false;
    }
    final Student student = other;
    return name == student.name
        && age == student.age
        && school == student.school;
  }
}
void main(){
  Student studentA = Student.create('小白', 18, '南京大学');
  Student studentB = Student.create('小白', 18, '南京大学');
  print(studentA == studentB);
  // true
}
上一篇下一篇

猜你喜欢

热点阅读