Learning Swift from scratch (Day 42)--constructor call rule

Source: Internet
Author: User

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

You can use the constructor proxy in a constructor to help complete some of the construction work. Class constructor proxies are divided into horizontal and upward proxies, which can only occur inside the same class, and this constructor is called a convenience constructor. An up-agent occurs in the case of inheritance, in which the parent class constructor is called to initialize the stored property of the parent class, which is called the specified constructor, during the child class construction.  

Constructor Call rules

Person and the Student Class Example:

Class person {    var name: string    var age :  int        func description ()  -> String  {        return  "\ (name)   Age is:  \"      }    convenience init  ()  {         //Convenient constructor         self.init (name:  "Tony")          self.age = 18    }     convenience init  (name: string)  {//convenient constructors          Self.init (name: name, age: 18)     }    init  (name:  string, age: int) {       //Specify constructors          self.name = name        self.age  = age     }} class Student: Person {    var school:  string    init  (name: string, age: int,school: string)  {        //the specified constructor         self.school  = school        super.init (name: name, age:  Age)     }    convenienceoverride init  (name: string,age :  int)  {//Convenient constructor         self.init (name: name, age:  age, school:  "Tsinghua University")     }} let student = student () Print ("Student:  \ (Student.description ())")


Calls between constructors form a chain of constructor functions.

Swift rules that restrict proxy calls between constructors are 3 bar, as shown below.

  • specifies that the constructor must call the specified constructor of its immediate parent class. visible from the diagram, the ④ number in Student specifies that the constructor calls the ③ number in the person specified by the constructor.

  • the convenience constructor must call other constructors defined in the same class. as seen from the diagram, the ⑤ in Student facilitates constructors to call the ④ number in the same class to facilitate constructors, person The ① number in the convenient constructor calls the ② number in the same class to facilitate the constructor.

  • the convenience constructor must eventually end with a call to a specified constructor function. as seen from the diagram, the ⑤ in Student facilitates the constructor to call the ④ number in the same class to specify the constructor, the person The ② number in the convenient constructor calls the ③ number in the same class to specify the constructor.

650) this.width=650; "Width=" 550 "height=" 417 "title=" image1.jpg "style=" WIDTH:550PX;HEIGHT:417PX; "src="/HTTP/ S4.51cto.com/wyfs02/m00/7c/d6/wkiom1bzadzwdrxkaaomy-gyifo215.jpg "border=" 0 "vspace=" 0 "hspace=" 0 "alt=" Wkiom1bzadzwdrxkaaomy-gyifo215.jpg "/>

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/M00/7C/D5/wKioL1bZAImBkzYzAAAs2MBEZnc713.png "alt=" Wkiol1bzaimbkzyzaaas2mbeznc713.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/1747498

Learning Swift from scratch (Day 42)--constructor call rule

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.