Learning Swift from scratch (day 41)-Inheritance of classes

Source: Internet
Author: User

original articles, welcome reprint. Reprint Please specify: Dongsheng's blog

Swift inheritance can only occur on a class and cannot occur on enumerations and structs. A class can inherit a method, property, subscript, and other characteristics of another class, and when a class inherits from another class, the inheriting class is called a subclass, and the inherited class is called the parent class (or superclass). After the subclass inherits the parent class, you can override the properties of the parent class's methods, attributes, subscripts, and so on.  

to understand inheritance, look at a scenario where an object-oriented programmer, Xiao Zhao, needs to describe and process personal information during programming, so he defines the class Person , as shown below:

Class Person {var name:string var age:int func description (), String {return "\ (name) age is: \ ( Age) '} init () {name = "" Age = 1}}


a week later, Xiao Zhao met new needs and needed to describe and process student information, so he defined a new class Student , as shown below:

Class Student {var name:string var age:int var school:string func description ()-String { Return "\ (name) is: \ (age)"} init () {school = "" "" Name = ""


Many people will think that Xiao Zhao's approach can understand and believe that this is feasible, but the problem is Student and the Person the structure of the two classes is too close, the latter is only one more property than the former School , but to repeat the definition of all other content, it really makes people "not reconciled". Swift provides a mechanism to solve similar problems, that is , the inheritance of Classes , the code is as follows:

Class Student:person {var school:string overrideinit () {school = "" super.init () Age = 8 }}


Studentclass inherits the Personall features in the class, ":"After the PersonThe class is the parent class. Swiftthe class in can have no parent class, for example Personclass, defined by the time after the ":"This is not a parent classbase class.

also override Init () is a subclass that overrides the parent class constructor.

in general, a subclass can inherit only one parent class, which is called Single Inheritance , but there are cases where a subclass can have multiple different parent classes, which is called Multiple Inheritance . In Swift , the inheritance of a class can only be single-inheritance. Multiple inheritance can be implemented by complying with multiple protocols. In other words, in Swift , a class can inherit only one parent class, but it is possible to follow multiple protocols.

Welcome to follow Dongsheng Sina Weibo@tony_Dongsheng.
Learn about the latest technical articles, books, tutorials and information on the public platform of the smart Jie classroom
650) this.width=650; "title=" 00.png "src=" http://s4.51cto.com/wyfs02/M01/7C/D6/wKiom1bY_ 4mstw94aaas2mbeznc786.png "alt=" Wkiom1by_4mstw94aaas2mbeznc786.png "/>More ProductsIOS,Cocos, mobile Design course please pay attention to the official website of Chi Jie Classroom:http://www.zhijieketang.com
Smart-Jie Classroom Forum Website:http://51work6.com/forum.php


This article is from the "Dongsheng-ios Technical Consultant" blog, make sure to keep this source http://tonyguan.blog.51cto.com/701759/1747494

Learning Swift from scratch (day 41)-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.