Swift-inheritance, swift inheritance

Source: Internet
Author: User

Swift-inheritance, swift inheritance

A class can inherit the methods, attributes, and other features of another class. When a class inherits other classes, the inherited class is called a subclass, And the inherited class is called a superclass (or parent class ). In Swift, inheritance has the characteristics of single inheritance. each subclass has only one direct parent class. inheritance is a basic feature of partition classification and other types.

In Swift, classes can call and access methods, attributes, and subscript scripts of the parent class, and rewrite these methods to optimize or modify the behavior of attributes and subscript scripts. Swift checks whether your override definition has a matching definition in the parent class to ensure that your rewrite behavior is correct. You can add an attribute observer for the inherited attributes in the class. When the attribute value changes, the class will be notified. You can add an attribute observer for any attribute, whether it is originally defined as a storage attribute or a computing attribute.

1. Define a base class

Classes that do not inherit from other classes are called base classes. (Classes in Swift are not inherited from a general base class. If you do not specify a superclass for your defined class, the class will automatically become the base class .)

Class Fruit {var weight = 0.0 func info () {print ("I Am a Fruit, heavy \ (weight)")} var f = Fruit () f.info () // I am a fruit, weighing 0.0
2. Subclass generation

Subclass generation refers to creating a new class based on an existing class. Subclass inherits the features of the parent class and can be optimized or changed. You can also add new features for the subclass. To specify the superclass of a class, write the superclass name behind the subclass name and separate it with a colon:

Class SomeClass: SomeSuperClass {// class definition}

Define an Apple class and inherit from Fruit

Class Apple: Fruit {var name: String! Func taste () {print ("\ (name) taste good")} var a = Apple (). weight = 3.2a.name = "yellow banana" a.info () // I am a fruit, heavy 3.2a.taste () // yellow banana taste good
3. Rewrite

Subclasses can provide their own implementations for inherited instance methods, class methods, instance attributes, or subscript scripts. This behavior is called rewriting. When a subclass overrides the method of the parent class, override must be used.

When the subclass overrides the method, attribute, or subscript script of the parent class, the subclass cannot access the content covered by the parent class. In this case, you can use the super prefix to access the method of the parent class version, attribute and subscript script.

3.1 rewrite Method

The Apple class overrides the info method of the parent class, calls super.info, and prints it twice.

Class Apple: Fruit {var name: String! Func taste () {print ("\ (name) taste good")} override func info () {super.info () print ("\ (name) weight is \ (weight) ")} var a = Apple (). weight = 3.2a.name = "yellow banana" a.info () // I am a fruit, weight 3.2 yellow banana weight is 3.2
3.2 rewrite attributes

Swift allows you to provide custom getter (or setter) to override any inherited attributes, whether they are stored or computed. The subclass does not know whether the inherited attribute is stored or computing. It only knows the name and type of the inherited attribute. Therefore, when you override an attribute, you must write its name and attribute. You can rewrite an inherited read-only attribute to a read/write attribute, but cannot rewrite an inherited read/write attribute to a read-only attribute.

Class Fruit {var weight = 0.0 var description: String {return "\ (self. weight)"} class Apple: Fruit {var name: String! Override var description: String {return self. name + "weight is" + super. description} var a = Apple (). weight = 4.0a.name = "yellow banana" print (. description) // yellow banana weight is 4.0
3.3 rewrite attribute observer

Do not add attribute observers for inherited constant storage attributes or inherited read-only computing attributes. The values of these attributes will not change.

Do not provide the rewritten setter and the rewritten attribute Observer at the same time. If you want to observe the changes in the attribute value and have provided the setter Method for that attribute, you can observe it in setter.

class Fruit {    var weight = 0.0}class Apple: Fruit {    var name : String!    override var weight: Double {        didSet {            super.weight += 1        }    }}var a = Apple()a.weight = 4.0print(a.weight) //5.0
3.4 rewrite subscript

Rewrite the subscript to provide customized getter and setter. When rewriting the subscripts, you can rewrite the read-only subscripts of the parent class to read/write subscripts, but you cannot rewrite the read/write subscripts of the parent class to read-only subscripts.

3.5 prevent Rewriting

Final modifier, which can be used to modify classes, attributes, methods, and subscripts. Classes modified with final cannot be inherited. Methods, attributes, and subscript scripts modified with final cannot be overwritten.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.