Swift - Comparable

作者: aven_kang | 来源:发表于2022-03-26 22:25 被阅读0次
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