Learning Swift from scratch (Day43)--Constructor inheritance

Source: Internet
Author: User

Original articles, welcome reprint. Reprint Please specify: Dongsheng's Blog

Swift There are two sources of the subclass constructors in: write themselves and inherit from the parent class. Not all constructors of the parent class can inherit, and constructors that inherit from the parent class are conditional, as shown below.

    • condition 1: If the subclass does not define any of the specified constructors, it automatically inherits the specified constructors for all the parent classes.

    • condition 2: If the subclass provides all of the parent class to specify the implementation of the constructor, whether inherited through condition 1, or by writing it yourself, it will automatically inherit all the convenience constructors of the parent class.

look at the example code below:

class person {                var name: String    var age: Int         func description ()  -> String {         return  "\ (name)   Age is:  \"     }     convenience init  ()  {        self.init (name:  "Tony ")         self.age = 18    }     convenience init  (name: string)  {         self.init (name: name, age: 18)     }    init  (Name: string, age: int) {               SELF.Name = name        self.age  = age     }} class student: person {    var school: string     init  (name: string, age: int,school: string)  {         self.school = school         super.init (name: name, age: age)     }     convenience overrideinit  (Name: string,age: int)  {           self.init (name: name, age: age, school:  "Tsinghua University")      }} class Graduate: Student {         var special: String =  ""}


let's see what's eligible.1the inheritance,GraduateInheritanceStudent,Graduateclass does not define any of the specified constructors, it automatically inherits allStudentthe specified constructor. Meet the criteria1after theGraduatefromStudentThe following specified constructor is inherited:

Init (name:string, age:int,school:string)


and look at the qualifying2the inheritance, due toGraduaterealized theStudentall of the specified constructors,Graduatewill automatically inherit allStudentthe convenient constructor for the function. Meet the criteria2after theGraduatefromStudentinherited the following3a convenient constructor:

Init (name:string, Age:int) init (name:string) init ()


Student Inheritance Person after a 4 a constructor function.

conditions1theStudentis not satisfied, because it has a specified constructor,Studentconvenience constructors in a classInit (name:string, Age:int)Meet the conditions2, which implements the parent class specifying the constructorInit (name:string, Age:int). Also, because the subclass constructor is the same as the parent class constructor parameter, you need to use theOverridekeyword that represents the subclass constructor override (overriding) The parent class constructor.

because Student class implements the parent class to specify the constructor, and therefore also inherits two other convenient constructors for the parent class.

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://s2.51cto.com/wyfs02/M02/7C/D6/wKiom1bZAMugLbLuAAAs2MBEZnc805.png "alt=" Wkiom1bzamuglbluaaas2mbeznc805.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/1747499

Learning Swift from scratch (Day43)--Constructor inheritance

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.