Swift 2.0 Learning Note (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: \ (Ages) "    }    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 {        ret Urn "\ (name) age is: \ (Ages)"    }    init () {        school = "" ""        name = ""    

Many people will think that Xiao Zhao's approach can understand and believe that this is feasible, but the problem isStudentand the Personthe structure of the two classes is too close, the latter is only one more property than the formerSchool, but to repeat the definition of all other content, it really makes people "not reconciled". Swiftprovides a mechanism for solving a similar problem, which isInheritance of Classes, the code looks like this:

Class Student:person {    var school:string    override Init () {        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 calledSingle Inheritance, but there are cases where a subclass can have multiple different parent classes, which is calledMultiple Inheritance. In theSwift, the inheritance of a class can only be single-inheritance. Multiple inheritance can be implemented by complying with multiple protocols. In other words,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
?
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

Swift 2.0 Learning Notes (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.