根据文档https://api.flutter.dev/flutter/painting/BoxShadow/operator_equals.html其实现如下
@override
bool operator ==(Object other) {
if (identical(this, other))
return true;
if (other.runtimeType != runtimeType)
return false;
return other is BoxShadow
&& other.color == color
&& other.offset == offset
&& other.blurRadius == blurRadius
&& other.spreadRadius == spreadRadius;
}
Run Code Online (Sandbox Code Playgroud)
和hashcode属性如下
@override
int get hashCode => hashValues(color, offset, blurRadius, spreadRadius);
Run Code Online (Sandbox Code Playgroud)
这实际上有什么作用?它有什么用处?代码中opeartor、runtimeType、等的用途是什么?hasCode如果您也能以更简单的方式提供一些示例,那就太好了。
当您需要比较两个对象/类的实际值时,此运算符很有用,因为 flutter 默认情况下会比较对象的实例,并且在这种情况下,即使两个对象的实际值相同,它们也永远不会相同,
例如,〜只需在DartPad.dev上运行以下示例
颤振默认情况:
void main() {
Person p1 = Person("StackOverFlow");
Person p2 = Person("StackOverFlow");
print("Both Classes are same: ${p1 == p2}"); // <- print 'false'
}
class Person {
String name;
Person(this.name);
}
Run Code Online (Sandbox Code Playgroud)
对于覆盖案例:
void main() {
Person p1 = Person("StackOverFlow");
Person p2 = Person("StackOverFlow");
print("Both Classes are same: ${p1 == p2}"); // <- print 'true'
}
class Person {
String name;
Person(this.name);
@override
bool operator ==(Object other) {
return (other is Person) && other.name == name;
}
}
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,请阅读这篇文章。
| 归档时间: |
|
| 查看次数: |
4647 次 |
| 最近记录: |