Original articles, welcome reprint. Reprint Please specify: Dongsheng's Blog
Constructors can also be overloaded as a special method.
Swift The constructors can be multiple, and their argument lists and return values can be different, and these constructors form overloads.
The sample code is as follows:
class rectangle { var width: double var height: double init (Width: double, height: double) { self.width = width self.height = height } init (W width: Double,H height: double) { self.width = width self.height = height } init (length: double) { self.width = length self.height = length } init () { self.width = 640.0 self.height = 940.0 } } var rectc1 =rectangle (width: 320.0, height: 480.0) print ("Rectangle : \ (rectc1.width) x\ (rectc1.height) ") var rectc2 = rectangle (W: 320.0, H: 480.0) print ("Rectangle: \ (rectc2.width) X\ (rectc2.height) ") var rectc3 =rectangle (length: 500.0) print ("Rectangle 3:\ (rectc3.width) x\ ( Rectc3.height) var rectc4 = rectangle () &Nbsp; print ("Rectangle 4:\ (rectc4.width) x\ ( Rectc4.height) ")
Constructor Proxy
to reduce code duplication among multiple constructors, you can define a constructor by calling other constructors to complete the partial construction of the instance, called Constructor Proxy . Constructor proxies are used differently in structs and classes, and the constructor proxies in structs are introduced first.
The example in the previous section is modified as follows:
struct rectangle { var width: double Var height: double init (width: Double, height: double) { self.width = width self.height = height } init (w width: double,h height: Double) { self.width = width self.height = height } init (length: double) { //called the Self.init statement self.init (w: length, h: length) } init () { // Called the Self.init statement self.init (width: 640.0, height: 940.0) } } var rectc1 =rectangle (width: 320.0, height: 480.0) print ("Rectangle: \ (rectc1.width) x\ (rectc1.height)") var rectc2 = Rectangle (w: 320.0, h: 480.0) Print ("Rectangle: \ (rectc2.width) x\ (rectc2.height)") var rectc3 =rectangle (length: 500.0) print ("Rectangular 3:\ ( Rectc3.width) x\ (Rectc3.height) var rectc4 = rectangle () print ("Rectangular 4:\ ( Rectc4.width) x \ (rectc4.height) ")
will be Rectangle declared as struct type, which also has 4 overload of the constructor function.
This is done in the same type Self.init statement to invoke other constructors of the current type, other constructors are called constructor proxies.
class constructor Horizontal Proxy
Because classes have inheritance relationships, class constructor proxies are more complex and are divided into horizontal and upward proxies.
Horizontal Agent similar to struct type constructor proxies, which occur inside the same class, such constructors are called Convenient constructor function (convenience initializers).
up Proxy In the case of inheritance, the parent class constructor is called to initialize the storage properties of the parent class during the subclass construction, which is called the Specifying Constructors (designated initializers).
The above example is modified as follows:
class rectangle { var width: double Var height: double init (width: Double, height: double) { self.width = width self.height = height } init (W width: Double,H height: double) { self.width = width self.height = height } convenience init (length: double) { self.init (w: length, h: length) } convenience init () { Self.init (width: 640.0, height: 940.0)     } } VAR RECTC1 =rectangle (width: 320.0, height: 480.0) print ("Rectangle: \ (rectc1.width) x\ ( Rectc1.height) var rectc2 = rectangle (w: 320.0, h: 480.0) print ("Rectangle: \ (rectc2.width) x\ (rectc2.height)") var rectc3 =rectangle (length: 500.0) print ("Rectangle 3:\ (rectc3.width) x\ (rectc3.height)") var rectc4 = rectangle () &Nbsp; print ("Rectangle 4:\ (rectc4.width) x\ (rectc4.height)")
will be Rectangle declared as classes, which also have 4 overload of the constructor function.
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://s4.51cto.com/wyfs02/M01/7C/BF/wKiom1bXnXSwo2_RAAAs2MBEZnc727.png "alt=" Wkiom1bxnxswo2_raaas2mbeznc727.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/1747009
Learn notes from scratch (day 39)-constructor overloading