12-Swift继承和重写

2020-12-17  本文已影响0人  一抹相思泪成雨

1.继承

2.重写实例方法、下标

class Animal {
    func speak() {
        print("Animal speek")
    }
    subscript(index: Int) -> Int {
        return index
    }
}

var anim: Animal
anim = Animal()
anim.speak()  // Animal speak
print(anim[6]) // 6

class Cat: Animal {
    override func speak() {
        super.speak()
        print("Cat speak")
    }
    override subscript(index: Int) -> Int {
        return super[index] + 1
    }
}

anim = Cat() // Animal speak
anim.speak() // Cat speak
print(anim[6]) // 7

/// 被class修饰的类型方法、下标,允许被子类重写
/// 被static修饰的类型方法、下标,不允许被子类重写

3.重写属性

4.重写实例属性

class Animal {
    func speak() {
        print("Animal speak")
    }
    subscript(index: Int) -> Int { 
      return index
    } 
}

var anim: Animal 
anim = Animal() 
anim.speak()  // Animal speak 
print(anim[6]) // 6

class Cat : Animal {
   override func speak() {
        super.speak()
        print("Cat speak")
    }
     override subscript(index: Int) -> Int {
        return super[index] + 1
    }
 }

anim = Cat() // Animal speak 
anim.speak() // Cat speak
print(anim[6])  // 7 

5.重写类型属性

 class Animal {
    class func speak() {
        print("Animal speak")
    }
    class subscript(index: Int) -> Int {
         return index
    } 
}
 Animal.speak()  // Animal speak
 print(Animal[6]) // 6 

class Cat : Animal {
    override class func speak() {
        super.speak()
        print("Cat speak")
     }
    override class subscript(index: Int) -> Int { 
        return super[index] + 1
    } 
}

Cat.speak() // Animal speak // Cat speak
print(Cat[6]) // 7 

6.重写属性

class Circle {
   var radius: Int = 0
   var diameter: Int {
    set {
           print("Circle setDiameter")
           radius = newValue / 2
       } get {
           print("Circle getDiameter")
           return radius * 2
       }
   } 
}

class SubCircle : Circle {
   override var radius: Int {
   set {
       print("SubCircle setRadius")
       super.radius = newValue > 0 ? newValue : 0
     } get {
       print("SubCircle getRadius")
       return super.radius }
   }
   override var diameter: Int {
       set {
       print("SubCircle setDiameter")
      super.diameter = newValue > 0 ? newValue : 0
     } get {
       print("SubCircle getDiameter")
       return super.diameter }
   } 
}

7.重写类型属性

class Circle1 {
    static var radius: Int = 0
    class var diameter: Int {
        set {
            print("Circle setDiameter")
        }
        get {
            print("Circle getDiameter")
            return radius * 2
        }
    }
}

class SubCircle1: Circle1 {
    override static var diameter: Int {
        set {
            print("SubCircle setDiameter")
            super.diameter = newValue > 0 ? newValue : 0
        }
        get {
            print("SubCircle getDiameter")
            return super.diameter
        }
    }
}

8.属性观察器

class TestCircle {
    var radius: Int = 1
}
class TestSubCircle: TestCircle {
    override var radius: Int {
        willSet {
            print("SubCircle willSetRadius", newValue)
        }
        didSet {
            print("SubCircle didSetRadius", oldValue, radius)
        }
    }
}
var testCircle = TestCircle()
// SubCircle willSetRadius 10
// SubCircle didSetRadius 1 10
testCircle.radius = 10

class TestCircle2 {
    var radius: Int = 1 {
        willSet {
            print("Cricle willSetRadius", newValue)
        }
        didSet {
            print("Circle didSetRaridus", oldValue, radius)
        }
    }
}
class TestSubCircle2: TestCircle2 {
    override var radius: Int {
        willSet {
            print("SubCircle willSetRadius", newValue)
        }
        didSet {
            print("SubCircle didSetRadius", oldValue, radius)
        }
    }
}

var testCircle2 = TestSubCircle2()
// SubCircle willSetRadius 10
// Circle willSetRadius 10
// Circle didSetRadius 1 10
// SubCircle didSetRadius 1 10
testCircle2.radius = 10

/// 3、
class Circle3 {
    var radius: Int {
        set {
            print("Circle setRadius", newValue)
        }
        get {
            print("Circle getRadius")
            return 20
        }
    }
}

class SubCircle3: Circle3 {
    override var radius: Int {
        willSet {
            print("SubCircle willSetRadius", newValue)
        }
        didSet {
            print("SubCircle didSetRadius", oldValue, radius)
        }
    }
}
var circle3 = SubCircle3()
// Circle getRadius
// SubCircle willSetRadius 10
// Circle setRadius 10
// Circle getRadius
// SubCircle didSetRadius 20 20
circle3.radius = 10

//4、
class Circle4 {
    class var radius: Int {
        set {
            print("Circle setRadius", newValue)
        }
        get {
            print("Circle getRadius")
            return 20
        }
    }
}
class SubCircle4: Circle4 {
    override static var radius: Int {
        willSet {
            print("SubCircle willSetRadius", newValue)
        }
        didSet {
            print("SubCircle didSetRadius", oldValue, radius)
        }
    }
}
// Circle getRadius
// SubCircle willSetRadius 10
// Circle setRadius 10
// Circle getRadius
// SubCircle didSetRadius 20 20
SubCircle4.radius = 10

9.final

上一篇 下一篇

猜你喜欢

热点阅读