Swift Learning Notes (iv) Inheritance of classes

Source: Internet
Author: User

In swift, inheritance is the basic feature of district classification and other objects

Inherited subclasses can override methods of the parent class, including class methods and instance methods, properties, and satellite scripts (subscript)

In the inheritance process, the constructor method Init () is not inherited and needs to be displayed

Class Tandem:bicycle {init () {super.init () Maxpassengers = 2}
}

Subclasses can only change the properties of variables inherited from the parent class, and cannot change the constant let property


About overriding override

Overriding method: The ability to override the parent class by using the Super.function method

Overriding properties:

When you rewrite a property, it is necessary to write its name and type. This enables the compiler to check that the property you override is matched to a property of the same type in the superclass.

After you provide a setter method in the Override property, you must provide a Get method


Override Property Watcher: Applies to variables, and is either a variable or a read-only computed property.

Because constants and read-only types are not able to be altered. So there is no need to see if its value will change.

At the same time. It is not possible to provide rewrite setters and override property observers at the same time. Because both are meant to observe the change in value. With the setter method, the quality changes can be monitored in real time without the need for a property observer to see if the value changes.


Assume that you do not want some methods, properties, and subscript to be overridden. By adding @final in front, you can


Swift Learning Notes (iv) Inheritance of classes

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.