The best Swift Inheritance Tutorial In 2024, In this tutorial you can learn Base class,Subclass,Rewrite (Overriding),Overriding methods and properties,Overriding Properties Viewer,Prevent rewrite,
Inheritance can be understood as a class we get the methods and properties of another class.
When a class inherits from another class, the derived class is called a subclass, the inherited class called superclass (or parent)
In Swift, the classes can call and access the superclass's methods, properties, and subscript script, and can override them.
We can also add properties observer inherited property for the class.
No other classes inherit a class, call the base class (Base Class).
The following example, we define a base class StudDetails, describes the student (stname) and all subjects scores (mark1, mark2, mark3):
class StudDetails { var stname: String! var mark1: Int! var mark2: Int! var mark3: Int! init(stname: String, mark1: Int, mark2: Int, mark3: Int) { self.stname = stname self.mark1 = mark1 self.mark2 = mark2 self.mark3 = mark3 } } let stname = "swift" let mark1 = 98 let mark2 = 89 let mark3 = 76 print(stname) print(mark1) print(mark2) print(mark3)
The above program execution output is:
swift 98 89 76
swift 98 89 76
Refers to a subclass is to create a new class based on an existing class.
To specify the superclass of a class, the super class name written on the back of the subclass name, (:) colon-separated syntax is as follows
class SomeClass: SomeSuperclass { // 类的定义 }
The following example, we define a superclass StudDetails, then subclass Tom inherit it:
class StudDetails { var mark1: Int; var mark2: Int; init(stm1:Int, results stm2:Int) { mark1 = stm1; mark2 = stm2; } func show() { print("Mark1:\(self.mark1), Mark2:\(self.mark2)") } } class Tom : StudDetails { init() { super.init(stm1: 93, results: 89) } } let tom = Tom() tom.show()
The above program execution output is:
Mark1:93, Mark2:89
Subclass can be inherited instance methods, class methods, instance, property, or subscript script to realize their customization capabilities, we call this behavior rewritten (overriding).
We can use the override keyword to achieve rewrite.
The method you can use to access the prefix super super class, property or subscript script.
Rewrite | Access methods, properties, subscript scripts |
---|---|
method | super.somemethod () |
Attributes | super.someProperty () |
Subscript script | super [someIndex] |
In our sub-class, we can use the override keyword to override the superclass method.
The following examples, we rewrite the show () method:
class SuperClass { func show() { print("这是超类 SuperClass") } } class SubClass: SuperClass { override func show() { print("这是子类 SubClass") } } let superClass = SuperClass() superClass.show() let subClass = SubClass() subClass.show()
The above program execution output is:
这是超类 SuperClass 这是子类 SubClass
You can provide a custom getter (or setter) to rewrite any inherited property, whether the property is inherited to a storage type or computing type of property.
Subclass does not know inherited property storage type or type of calculation, it only knows inherited property will have a name and type. So you rewrite a property, it is necessary to name and type written down.
be careful:
If you provide setter in rewriting properties, then you must provide getter.
If you do not want getter rewritten version where to modify inherited property values, you can return to the values inherited by super.someProperty, which is the name of your someProperty to rewrite the attributes.
The following example we define a subclass of superclass Circle and Rectangle, we rewrite the Rectangle class property area:
class Circle { var radius = 12.5 var area: String { return "矩形半径 \(radius) " } } // 继承超类 Circle class Rectangle: Circle { var print = 7 override var area: String { return super.area + " ,但现在被重写为 \(print)" } } let rect = Rectangle() rect.radius = 25.0 rect.print = 3 print("Radius \(rect.area)")
The above program execution output is:
Radius 矩形半径 25.0 ,但现在被重写为 3
You can add a property to inherit property in the properties observed in the rewrite. As a result, when the inherited property value changes, you will be monitored.
Note: You can not add properties observer for the inherited type attribute constants memory or read-only computational inherited property.
class Circle { var radius = 12.5 var area: String { return "矩形半径为 \(radius) " } } class Rectangle: Circle { var print = 7 override var area: String { return super.area + " ,但现在被重写为 \(print)" } } let rect = Rectangle() rect.radius = 25.0 rect.print = 3 print("半径: \(rect.area)") class Square: Rectangle { override var radius: Double { didSet { print = Int(radius/5.0)+1 } } } let sq = Square() sq.radius = 100.0 print("半径: \(sq.area)")
半径: 矩形半径为 25.0 ,但现在被重写为 3 半径: 矩形半径为 100.0 ,但现在被重写为 21
We can use the final keyword prevents them from being overwritten.
If you override a final method, property, or subscript script, at compile time error.
You can add the final before the keyword class characteristic (final class) to the entire class is marked final, so the class can not be inherited, otherwise compilation errors will be reported.
final class Circle { final var radius = 12.5 var area: String { return "矩形半径为 \(radius) " } } class Rectangle: Circle { var print = 7 override var area: String { return super.area + " ,但现在被重写为 \(print)" } } let rect = Rectangle() rect.radius = 25.0 rect.print = 3 print("半径: \(rect.area)") class Square: Rectangle { override var radius: Double { didSet { print = Int(radius/5.0)+1 } } } let sq = Square() sq.radius = 100.0 print("半径: \(sq.area)")
Due to the above example uses the final keyword is not allowed to rewrite, so execution will be given:
error: var overrides a 'final' var override var area: String { ^ note: overridden declaration is here var area: String { ^ error: var overrides a 'final' var override var radius: Double { ^ note: overridden declaration is here final var radius = 12.5 ^ error: inheritance from a final class 'Circle' class Rectangle: Circle { ^