美文网首页
Swift基础知识之继承

Swift基础知识之继承

作者: alfei13 | 来源:发表于2021-05-29 22:51 被阅读0次

继承(Inheritance)

学习新知识,总是在不经意间给忘记,俗话说好记性不如烂笔头,谨以此记录,方便日后用到时查找,同时如果能给需要的朋友一份灵感,也将对我的莫大鼓励!有不足的地方还请不吝赐教,感谢!!!

1. 继承的简单说明

  • 值类型(枚举、结构体)不支持继承,只有类支持继承

  • 没有父类的类,称为:基类

    • swift并没有像OC、Java那样的规定:任何类最终都要继承某个基类

    Swift classes do not inherit from a universal base class. Classes you define without specifying a superclass automatically become base classes for you to build upon

  • 子类可以重写父类的下标、方法、属性,重写必须加上override关键字

2. 重写实例方法、下标

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

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

var anima: Animal = Animal()
// Animal speak
anima.speak()
// anima[6] = 6
print("anima[6] = \(anima[6])")

anima = Cat()
// Animal speak
// Cat speak
anima.speak()
// anima[6] = 7
print("anima[6] = \(anima[6])")

3. 重写类型方法、下标

  • 被static修饰的方法、下标允许被子类重写
  • 被class修饰的方法、下标允许被子类重写
class Animal {
    class func speak() {
        print("Animal speak")
    }
    class subscript(index: Int) -> Int {
        index
    }
}

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

// Animal speak
Animal.speak()
// anima[6] = 6
print("anima[6] = \(Animal[6])")

// Animal speak
// Cat speak
Cat.speak()
// anima[6] = 7
print("anima[6] = \(Cat[6])")

4. 重写属性

  • 子类可以将父类的属性(存储、计算)重写为计算属性
  • 子类不可以将父类属性重写为存储属性
  • 只能重写var属性,不能重写let属性
  • 重写时,属性名、类型要一致
  • 子类重写后的属性权限,不能小于父类属性的权限
    • 如果父类属性时只读的,那么子类重写后的属性可以是只读的,也可以是可读写的
    • 如果父类属性时可读写的,那么子类重写后的属性也必须是可读写的
// 父类
class Circle {
    var radius: Int = 0
    var diameter: Int {
        set {
            print("Circle setDiameter")
            radius = newValue / 2
        }
        get {
            print("Circle getDiameter")
            return radius * 2
        }
    }
}

var circle: Circle
circle = Circle()
circle.radius = 6
// Circle getDiameter
// 12
print(circle.diameter)

//Circle setDiameter
//10
circle.diameter = 20
print(circle.radius)

// 子类
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 / 2 > 0 ? newValue : 0
        }
        get {
            print("SubCircle getDiameter")
            return super.diameter
        }
    }
}

circle = SubCircle()

//SubCircle setRadius
circle.radius = 6

//SubCircle getDiameter
//Circle getDiameter
//SubCircle getRadius
//12
print(circle.diameter)

//SubCircle setDiameter
//Circle setDiameter
//SubCircle setRadius
circle.diameter = 20

//SubCircle getRadius
//10
print(circle.radius)

// 上述注释内容为打印结果

5. 属性观察器

  • 可以在子类中为父类属性(除了只读计算属性、let属性)增加属性观察器
class Circle {
    var radius: Int = 1
}

class SubCircle : Circle {

    override var radius: Int {
        willSet {
            print("SubCircle willSetRadius", newValue)
        }
        didSet {
            print("SubCircle didSetRadius", oldValue, radius)
        }
    }
}

var circle = SubCircle()

//SubCircle willSetRadius 10
//SubCircle didSetRadius 1 10
circle.radius = 10
  • 给父类的计算属性添加属性观察器、override需要加上
class Circle {
    var radius: Int  {
        set {
            print("Circle setRadius", newValue)
        }
        get {
            print("Circle getRadius")
            return 20
        }
    }
}

class SubCircle : Circle {

    override var radius: Int {
        willSet {
            print("SubCircle willSetRadius", newValue)
        }
        didSet {
            print("SubCircle didSetRadius", oldValue, radius)
        }
    }
}

var circle = SubCircle()

//Circle getRadius
//SubCircle willSetRadius 10
//Circle setRadius 10
//Circle getRadius
//SubCircle didSetRadius 20 20
circle.radius = 10

6. final

  • 被final修饰的方法、下标、属性、禁止被重写
  • 被final修饰的类、禁止被继承

相关文章

  • swift学习资料整理

    基础知识篇 Swift Programming Language Using Swift with Cocoa a...

  • Swift学习之继承

    1、值类型(枚举、结构体)不支持继承,只有类支持继承。 类被关键字final修饰后,就不会被任何类继承 2、子类可...

  • OC Swift文件混编

    一.Swift 类可以继承 OC 类,OC 类不能继承 Swift 类。 二.Swift 和 OC 混编 三.两个...

  • Swift教程目录

    1.Swift基础知识

  • oc 继承 swift 类

    oc 继承 swift 类 项目中使用 oc 继承 swift 类编译器报错: oc 继承前增加 objc_sub...

  • OC调用Swift

    OC调用Swift,Swift的类必须继承NSObject

  • Swift枚举高级用法(Enum)

    关于swift的枚举 一 swift对于枚举的扩展性(Enum) 枚举的继承(继承任何类和协议,目前除了swift...

  • iOS之Scanner基本用法

    iOS之Scanner字符串扫描类 注: 本文主要语法为Swift4.0 Scanner继承自NSObject,...

  • 类继承

    继承是面向对象的重要特征之一。swift中的继承只能发生在类上,结构体和枚举不能继承。在swift中,一个类继承另...

  • Swift 5.x - 初始化(中文文档)

    引言 继续学习Swift文档,从上一章节:继承,我们学习了Swift继承相关的内容,如继承的作用、重写父类的方法和...

网友评论

      本文标题:Swift基础知识之继承

      本文链接:https://www.haomeiwen.com/subject/omtvsltx.html