original articles, welcome reprint. Reprint Please specify: Dongsheng's blog
When you extend a type, you can also add a new constructor. Value types differ from reference type extensions. Value types include types other than classes, primarily enumeration types and struct types.
Value type extension constructors
Example of a constructor defined in an extended struct type:
struct rectangle { var width : double var height : double init (width : double, height : double) { self.width = width self.height = height } }extension rectangle { // Defines the extension type of rectangle init (length : double) { self.init (width : length, height :length) }} var rect = rectangle (width: 320.0, height : 480.0) //calls the constructor of two arguments, which is provided by the original type, ReThe Ctangle type is already an extension type print ("Rectangle: \ (rect.width) x \ (rect.height)") var square =rectangle (length: 500.0) //invokes the constructor of a parameter, which is the type of extension provided by the print ("Square: \ (square.width) x \ ( Square.height) ")
Self.init is a constructor that calls two parameters of the original type.
References type extension Constructors
Example of defining constructors in an extension class:
class person { var name : string var age : int func description () -> String { return "\ (name) Age is: \" } init (Name : string, age : int) { self.name = name self.age = age }} extension Person { //defines the extension type of the person class convenience init (name : string) { //Convenient constructor self.init ( Name : name,&nbsP;AGE : 8) }} let p1 = person ( name : "Mary") //calls the constructor of the two parameter, which is provided by the original type, when the person type is already an extension type. Print ("Person1 : \ (P1.description ())") Let p2 = person (name : "Tony", age  : 28)//The constructor that invokes a parameter creates a person instance, which is provided by the extension type. Print ("Person2 : \ (P2.description ())")
Code Self.init (Name:name, Age:8) invokes the specified constructor proxy section of the construction task.
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://s5.51cto.com/wyfs02/M00/7C/EF/wKioL1bc8XHySeiBAAAs2MBEZnc010.png "alt=" Wkiol1bc8xhyseibaaas2mbeznc010.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/1748298
Learn notes from scratch (day 51)--extended constructors