Swift constructor overload

Source: Internet
Author: User

Swift constructor overload
Like a function, methods are overloaded, and the method of overloading is the same as that of functions. Is there any overload as a special method of the constructor? The answer is yes.
I. constructor overload Concept
The condition for function overloading in Swift is also applicable to constructors. The conditions are as follows:
Functions have the same name;
Different parameter lists, different return value types, or different external parameter names;
The constructor in Swift can meet the following two conditions:

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) ⑤ println (Rectangle :( rectc1.width) x (rectc1.height) var rectc2 = Rectangle (W: 320.0, h: 480.0) ⑥ println (Rectangle :( rectc2.width) x (rectc2.height) var rectc3 = Rectangle (length: 500.0) ⑦ println (Rectangle 3 :( rectc3.width) x (rectc3.height )) var rectc4 = Rectangle () returns println (Rectangle 4 :( rectc4.width) x (rectc4.height ))


Code ① ~ Line 4 defines four constructors, and the others are heavy loads. In terms of the number and type of parameters, the constructor of line ① and line ② are the same, but their external parameter names are different, therefore, row ⑤ calls the constructor of row ①, and row ⑥ calls the constructor of row ②.
The number of constructor parameters of rows ③ and ④ is different from that of row ①. Therefore, the constructor of row ③ is called in row 7th, the fourth line calls the constructor of the nth line.
Ii. Value Type constructor proxy
To reduce code duplication between constructors, you can call other constructors to complete some of the constructor processes. The constructor proxy uses different methods in the value type and reference type. This section describes the Value Type constructor proxy.
Modify the example in the previous section 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) {③ self. init (W: length, H: length)} init () {④ self. init (width: 640.0, height: 940.0) }}var rectc1 = Rectangle (width: 320.0, height: 480.0) ⑤ println (Rectangle :( rectc1.width) x (rectc1.height )) var rectc2 = Rectangle (W: 320.0, H: 480.0) ⑥ println (Rectangle :( rectc2.width) x (rectc2.height) var rectc3 = Rectangle (length: 500.0) 7. println (Rectangle 3 :( rectc3.width) x (rectc3.height) var rectc4 = Rectangle () returns println (Rectangle 4 :( rectc4.width) x (rectc4.height ))

Declare Rectangle as a struct type, and four constructors are reloaded. Self is used in the constructor of Line 3 and line 4. init statement, self indicates the current instance itself, init is its constructor, the self of the ③ line. the init (W: length, H: length) statement is used to call the constructor defined in line ②, And the self of line ④. the init (width: 640.0, height: 940.0) statement is used to call the constructor defined in row ①.
In the same type, the self. init statement is called as the constructor proxy.
Iii. Reference Type constructor horizontal proxy
The Reference Type constructor proxy is the class constructor proxy. Because classes have an inheritance relationship, the class constructor proxy is complex and can be divided into horizontal proxy and upward proxy.
The horizontal proxy is similar to the Value Type constructor proxy, which occurs within the same class. This convenience initializers is called convenience initializers ).
When the upstream proxy is inherited, you must first call the parent class constructor during the subclass construction process to initialize the storage attributes of the parent class. This constructor is called the specified Constructor (designated initializers ).
As we have not introduced inheritance, this chapter only introduces horizontal proxy.
Modify the example in the previous section 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) ⑤ println (Rectangle :( rectc1.width) x (rectc1.height )) var rectc2 = Rectangle (W: 320.0, H: 480.0) ⑥ println (Rectangle :( rectc2.width) x (rectc2.height) var rectc3 = Rectangle (length: 500.0) 7. println (Rectangle 3 :( rectc3.width) x (rectc3.height) var rectc4 = Rectangle () returns println (Rectangle 4 :( rectc4.width) x (rectc4.height ))

Declare Rectangle as a class. Four constructors are also overloaded. Self is used in the constructor of Line 3 and line 4. init statement, and the convenience keyword is added before the convenience, convenience indicates the constructor, Which means we define the constructor as a horizontal proxy to call other constructor.

Self. the init (W: length, H: length) statement is used to call the constructor proxy defined in row ② horizontally, and the self of row ④. the init (width: 640.0, height: 940.0) statement is used to call the constructor proxy defined in row ① horizontally.

 

 

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.