As with functions, methods are overloaded and overloaded in a way that is consistent with functions. So, as a special method of the constructor, are there overloads too? The answer is yes.
First, constructor overloading concept
The conditions for function overloading in Swift also apply to constructors, as follows:
The function has the same name;
The parameter 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:
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 () ⑧
println ("Rectangle 4: \ (rectc4.width) x \ (rectc4.height)")
The above Code section ①~④ defines 4 constructors, others are overloaded. From the number of arguments and parameter types, the constructors for line ① and line ② are the same, but their external parameter names are different, so the constructor for the ① row is called in line ⑤, and the first ⑥ row is called the constructor of the ② row.
The number of constructor arguments for line ③ and line ④ is different from line ①, so the constructor for the ③ row is called in line ⑦, and the first ④ row is called the constructor of the ⑧ row.
Second, value type constructor agent
To reduce code duplication between multiple constructors, when you define a constructor, you can call other constructors to complete the partial construction of the instance, which is called the constructor agent. The constructor agent is used differently in value types and reference types, and in this section we first describe the value type constructor proxies.
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) {③
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) ⑦
println ("Rectangle 3: \ (rectc3.width) x \ (rectc3.height)")
var rectc4 = Rectangle () ⑧
println ("Rectangle 4: \ (rectc4.width) x \ (rectc4.height)")
Declare rectangle as a struct type, with 4 constructors also loaded. The Self.init statement is used in the constructors of line ③ and ④, and self indicates the current instance itself, and Init is the constructor of itself, and the Self.init (w:length, h:length) Statement of the ③ row is the constructor that is defined in the invocation of the ② row. The Self.init (width:640.0, height:940.0) Statement of Line ④ is the constructor defined in the invocation of the ① row.
This invocation in the same type through the Self.init statement is what we call the constructor agent.
Third, reference type constructor horizontal proxy
The reference type constructor agent is the class constructor agent. Because classes have inheritance relationships, class constructor proxies are more complex and are divided into horizontal and upward proxies.
A horizontal proxy is similar to a value type constructor agent, which occurs inside the same class, which is called a convenience constructor (convenience initializers).
The up agent occurs in the case of inheritance, in which the parent class constructor is called first, and the stored property of the parent class is initialized, which is called the specified constructor (designated initializers).
Since we have not yet introduced inheritance, this chapter covers only horizontal proxies.
The example in the previous section 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) ⑤
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 () ⑧
println ("Rectangle 4: \ (rectc4.width) x \ (rectc4.height)")
Declare rectangle as a class, which also contains 4 constructors. The Self.init statement is used in the constructors of line ③ and ④, and the convenience keyword is prepended to the constructor, convenience represents the convenience constructor, which means that we define the constructor as a horizontal proxy call to the other constructors.
The Self.init (w:length, h:length) statement in line ③ is the constructor agent defined in the horizontal call to the ② row, and the ④ (Self.init, width:640.0) Statement of the height:940.0 row is the constructor agent defined in the horizontal call to the ① row.
For more information, please visit the first Swift book "Swift Development Guide" book Exchange discussion website: http://www.51work6.com/swift.php Welcome to join Swift Technical discussion group: 362298485
Welcome to Luxgen iOS Classroom public Platform
Swift constructor overloading