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