As with functions, methods also have overloads that are overloaded in the same way as functions. Is there an overload, then, as a special method of constructors? The answer is yes.
First, constructor overload concept
The condition of the function overload in Swift also applies to the constructor, as follows:
The function has the same name;
The argument list is different or the return value type is different, or the external parameter name is different;
The constructors in Swift can meet the following two conditions, the code is as follows:
Copy Code code 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) ⑤
println ("Rectangular: \ (rectc1.width) x \ (rectc1.height)")
var rectc2 = Rectangle (w:320.0, h:480.0) ⑥
println ("Rectangular: \ (rectc2.width) x \ (rectc2.height)")
var rectc3 = Rectangle (length:500.0) ⑦
println ("Rectangular 3:\ (rectc3.width) x \ (rectc3.height)")
var rectc4 = Rectangle () ⑧
println ("Rectangular 4:\ (rectc4.width) x \ (rectc4.height)")
The above code, line ①~④, defines 4 constructors, others are overloaded relationships. From the number of parameters and types of arguments, the ① row and the constructor of the ② row are the same, but their external parameter names are different, so the ⑤ row is called the constructor of the ① row, and the ⑥ row is invoked by the constructor of the ② row.
The number of constructor arguments for the ③ and ④ rows is different from the ① row, so the constructor for the ③ row is invoked on the ⑦ line, and the ④ row is invoked by the constructor of the ⑧ row.
Second, value type constructor agent
In order to reduce code duplication between multiple constructors, the process of partial construction of an instance can be accomplished by invoking other constructors, known as the constructor agent, when the constructor is defined. Constructor agents are used differently in value types and reference types, and we introduce the value type constructor agent first in this section.
Modify the example in the previous section as follows:
Copy Code code 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 ("Rectangular: \ (rectc1.width) x \ (rectc1.height)")
var rectc2 = Rectangle (w:320.0, h:480.0) ⑥
println ("Rectangular: \ (rectc2.width) x \ (rectc2.height)")
var rectc3 = Rectangle (length:500.0) ⑦
println ("Rectangular 3:\ (rectc3.width) x \ (rectc3.height)")
var rectc4 = Rectangle () ⑧
println ("Rectangular 4:\ (rectc4.width) x \ (rectc4.height)")
The rectangle is declared as a struct type, with 4 constructs being reset. The Self.init statement is used in the constructor of the ③ row and the ④ row, and self indicates that the current instance itself, Init is its own constructor, and the Self.init (w:length, h:length) Statement of the ③ line is the constructor defined in the ② row. The Self.init (width:640.0, height:940.0) Statement of the ④ line is the constructor that is defined in the call to the ① row.
This invocation of the Self.init statement in the same type is what we call the constructor agent.
Iii. reference type Builder Horizontal Proxy
A reference type constructor proxy is a class builder proxy. Because classes have inheritance relationships, class constructor agents are more complex and are divided into horizontal proxies and upward proxies.
The horizontal proxy is similar to the value type constructor proxy, which occurs inside the same class, which is called the convenience constructor (convenience initializers).
The upward proxy occurs under inheritance, in which the parent class constructor is invoked to initialize the storage properties of the parent class, called the specified constructor (designated initializers).
Since we have not yet introduced inheritance, this chapter only describes horizontal proxies.
Modify the example in the previous section as follows:
Copy Code code 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 ("Rectangular: \ (rectc1.width) x \ (rectc1.height)")
var rectc2 = Rectangle (w:320.0, h:480.0) ⑥
println ("Rectangular: \ (rectc2.width) x \ (rectc2.height)")
var rectc3 = Rectangle (length:500.0) ⑦
println ("Rectangular 3:\ (rectc3.width) x \ (rectc3.height)")
var rectc4 = Rectangle () ⑧
println ("Rectangular 4:\ (rectc4.width) x \ (rectc4.height)")
Declare rectangle as a class with 4 constructs to be loaded. The Self.init statement is used in the constructor of the ③ row and the ④ row, and the convenience keyword is added to the front of the constructor, convenience represents the convenience constructor, which means that we define the constructor as a horizontal proxy to invoke another constructor.
The Self.init (w:length, h:length) statement on the ③ line is a constructor agent that invokes the definition of the ② row horizontally, and the ④ (Self.init, width:640.0) Statement of the height:940.0 line is a constructor agent that invokes the definition of the ① row horizontally.
The problem of constructing the reset today, the small partners can refer to the example below, I hope to help you