// Equatable
// 要想得知2个实例是否等价,一般做法是遵守Equatable协议,重载:==运算符
// 与此同时,等价于重载了 != 运算符
// 告诉别人这个可以进行等号运算符进行计算的
struct Point: Equatable {
var x: Int, y: Int
}
var p1 = Point(x: 10, y: 20)
var p2 = Point(x: 11, y: 22)
print(p1 == p2)
print(p1 != p2)
// 如果数值是完全一样的,则是相等的
// Swift为以下类型提供默认的Equatable实现
// 没有关联类型的枚举
// 只拥有遵守Equatable协议关联类型的枚举
// 只拥有遵守Equatable协议存储属性的结构体
// 引用类型比较存储的地址值是否相等(是否引用着同一个对象),使用恒等运算符===、 !==
// Comparable
// score大的比较大,若score相等,age小的比较大
struct Student: Comparable {
var age: Int
var score: Int
init(score: Int, age: Int) {
self.score = score
self.age = age
}
static func < (lhs: Student, rhs: Student) -> Bool {
(lhs.score < rhs.score) || (lhs.score == rhs.score && lhs.age > rhs.age)
}
static func > (lhs: Student, rhs: Student) -> Bool {
(lhs.score > rhs.score) || (lhs.score == rhs.score && lhs.age > rhs.age)
}
static func <= (lhs: Student, rhs: Student) -> Bool {
!(lhs > rhs)
}
static func >= (lhs: Student, rhs: Student) -> Bool {
!(lhs < rhs)
}
}
// 要相比较2个实例的大小,一般做法是:
// 遵守Comparable协议
// 重载相应的运算符
var stu1 = Student(score: 100, age: 20)
var stu2 = Student(score: 98, age: 18)
var stu3 = Student(score: 100, age: 20)
print("___________________________")
print(stu1 > stu2)
print(stu1 >= stu2)
print(stu1 >= stu3)
print(stu1 <= stu3)
print(stu1 > stu2)
print(stu1 >= stu2)








网友评论