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
}