Swift - Comparable
class Student : Comparable {
static func < (lhs: Student, rhs: Student) -> Bool {
lhs.score < rhs.score
}
static func > (lhs: Student, rhs: Student) -> Bool {
lhs.score > rhs.score
}
static func == (lhs: Student, rhs: Student) -> Bool {
lhs.score == rhs.score
}
static func <= (lhs: Student, rhs: Student) -> Bool {
!(lhs > rhs)
}
static func >= (lhs: Student, rhs: Student) -> Bool {
!(lhs < rhs)
}
var score: Int
var age:Int
init(age:Int,score:Int) {
self.age = age
self.score = score
}
}
比较两个实例的大小
1.遵守Comparable
2.重载相应的运算符
本文标题:Swift - Comparable
本文链接:https://www.haomeiwen.com/subject/polerrtx.html
网友评论