Swift 2.0 Learning Note (Day43)--Constructor inheritance

Source: Internet
Author: User

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

There are two sources of child class constructors in Swift: writing and inheriting 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.

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

L Condition 2: If the subclass provides all the parent class to specify the implementation of the constructor, whether it is inherited through condition 1, or is implemented by itself, it will automatically inherit the convenience constructors of all the parent classes.

Here's the sample code:

classPerson {varname:stringvarage:int func description ()-String {return "\ (name) is: \ (age)"} convenience init () {self.init (name:"Tony") Self.age= -} convenience init (name:string) {Self.init (Name:name, Age: -)} init (name:string, age:int) {self.name=name Self.age=Age }} classStudent:person {varschool:string Init (name:string, Age:int, school:string) {Self.school=School Super.init (Name:name, Age:age)} convenienceOverrideInit (name:string, Age:int) {self.init (Name:name, Age:age, school:"Tsinghua University")    }} classgraduate:student {varSpecial:string =""}

To see the inheritance of condition 1, graduate inherited the Student,graduate class does not define any of the specified constructors, it automatically inherits the specified constructors for all student. After qualifying 1, graduate inherits from student the following specified constructor:

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

Again, with the inheritance of Condition 2, because graduate implements all the specified constructors for student, graduate automatically inherits all the student convenience constructors. After qualifying 2, graduate inherited the following 3 convenient constructors from student:

Init (name:string, Age:int)

Init (name:string)

Init ()

Student has 4 constructors after inheriting a person.

Condition 1 is not satisfied with the student because it has the specified constructor, the convenience constructor in the student class, Init (name:string, age:int) satisfies the condition 2, it implements the parent class to specify the constructor init (name:string, Age:int )。 In addition, because the subclass constructor is the same as the parent class constructor parameter, you need to use the Override keyword to represent the subclass constructor override (overriding) of the parent class constructor.

Because the student class implements the parent class to specify the constructor, it 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
?
More Products iOS, Cocos, mobile design courses please pay attention to the official website of Chi Jie Classroom: http://www.zhijieketang.com
Luxgen Classroom Forum Website: http://51work6.com/forum.php

Swift 2.0 Learning Note (Day43)--Constructor inheritance

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.