swift3.0 中class和static

 
 

class 和 static 相同点

1.可以修饰方法,static 修饰的方法叫做静态方法,class修饰的叫做类方法 2.都可以修饰计算属性

class 和 static 不同点

class 不能修饰存储属性 class 修饰的计算属性可以被重写,static 修饰的不能被重写 static 可以修饰存储属性,static修饰的存储属性称为静态变量(常量) static 修饰的静态方法不能被重写,class 修饰的类方法可以被重写 class 修饰的类方法被重写时,可以使用static 让方法变为静态方法 class 修饰的计算属性被重写时,可以使用static 让其变为静态属性,但它的子类就不能被重写了 class 只能在类中使用,但是static 可以在类,结构体,或者枚举中使用

class Person{
static var describe:String = " 这是一个人类"
class var score:Int{
    return 0
}
static var number:Int{
    return 100
}
// class 修饰的类方法可以被子类重写,static 修饰的静态方法不能被重写
class func  getScore()->Int{
    return score
}
static func aaa() -> Int {
    return 10
}
}

class Man: Person {

// 重写计算属性 可以使用static 来重写哦,但是static 重写后,就不能被它的子类再次重写了
static override var score:Int{
    return 1
}
/// 不能重写static修饰的计算性属性
//    static override var number:Int{
//        return 101
//    }
// 重写类方法时可以使用static 让其变成静态方法
 static override func  getScore()->Int{
    return score
}
/// 不能重写static修饰的方法
//    static override func aaa() -> Int {
//        return 10
//    }
}


猜你喜欢

转载自blog.csdn.net/feifeiwuxian/article/details/78444990
今日推荐