The Swift study note builder reloads

  • 2020-05-06 11:43:31
  • OfStack

Like functions, methods are overloaded in the same way as functions. Is there an overload for a particular method as a constructor? The answer is yes.

1. Constructor reload concept

The condition for function overloading in Swift also applies to the constructor, as follows:
Functions have the same name;
Different parameter list or return value type, or different external parameter name;
The constructor in Swift can satisfy the following two conditions, as shown in the code:


class Rectangle {
    var width : Double
    var height : Double
    init(width : Double, height : Double) {        1.
        self.width   = width
        self.height  = height
    }
    init(W width : Double,H height : Double) {        2.
        self.width   = width
        self.height  = height
    }
    init(length : Double) {        3.
        self.width   = length
        self.height  = length
    }
    init() {                                                (4)
        self.width   = 640.0
        self.height  = 940.0
    }
}
var rectc1 = Rectangle(width : 320.0, height : 480.0)        5.
println(" A rectangle :\(rectc1.width) x \(rectc1.height)")
var rectc2 = Rectangle(W : 320.0, H : 480.0)        6.
println(" A rectangle :\(rectc2.width) x \(rectc2.height)")
var rectc3 = Rectangle(length: 500.0)        All landowners
println(" A rectangle 3:\(rectc3.width) x \(rectc3.height)")
var rectc4 = Rectangle()        today
println(" A rectangle 4:\(rectc4.width) x \(rectc4.height)")

Line ~ above defines four constructors, the rest being overloading relationships. In terms of the number and type of arguments, line 1 and line 2 constructors are the same, but their external argument names are different, so line 5 calls the line 1 constructor, and line 6 calls the line 2 constructor.
The number of constructor parameters for the and rows is different from the row, so the row constructor is invoked in the row and the row constructor is invoked in the row.

Value type constructor proxy

To reduce code duplication between multiple constructors, when you define a constructor, you can complete part of the instance construction process by calling other constructors, which is called the constructor proxy. Constructor proxies are used differently in value types and reference types, and in this section we begin with the value type constructor proxy.
Modify the example from the previous section as follows:


struct Rectangle {
    var width : Double
    var height : Double
    init(width : Double, height : Double) {        1.
        self.width   = width
        self.height  = height
    }
    init(W width : Double,H height : Double) {        2.
        self.width   = width
        self.height  = height
    }
    init(length : Double) {        3.
        self.init(W : length, H : length)
    }
    init() {                                                (4)
        self.init(width: 640.0, height: 940.0)
    }
}
var rectc1 = Rectangle(width : 320.0, height : 480.0)        5.
println(" A rectangle :\(rectc1.width) x \(rectc1.height)")
var rectc2 = Rectangle(W : 320.0, H : 480.0)        6.
println(" A rectangle :\(rectc2.width) x \(rectc2.height)")
var rectc3 = Rectangle(length: 500.0)        All landowners
println(" A rectangle 3:\(rectc3.width) x \(rectc3.height)")
var rectc4 = Rectangle()        today
println(" A rectangle 4:\(rectc4.width) x \(rectc4.height)")

Declare Rectangle as a struct type, where four constructors are also reloaded. The self.init statement is used in the constructor of lines and, self indicates the current instance itself, init is its constructor, self.init (W: length, H: length) is calling the constructor defined in line, self.init (width: 640.0, height: 940.0) is calling the constructor defined in line.
This invocation in the same type through the self.init statement is what we call a constructor proxy.

iii. Reference type constructor horizontal proxy

The reference type constructor proxy is the class constructor proxy. Because of class inheritance, class constructor proxy is complex and can be divided into horizontal proxy and upward proxy.
              horizontal proxy is similar to the value type constructor proxy and occurs within the same class. This constructor is called a convenience constructor (convenience initializers).
              agent under the condition of inheritance, up in the process of subclasses structure to call the superclass constructor, initialize the parent class of storage properties, this constructor is called to specify a constructor (designated initializers).
Since we haven't covered inheritance yet, this chapter covers only horizontal proxies.
Modify the example from the previous section as follows:


class Rectangle {
    var width : Double
    var height : Double
    init(width : Double, height : Double) {        1.
        self.width   = width
        self.height  = height
    }
    init(W width : Double,H height : Double) {        2.
        self.width   = width
        self.height  = height
    }
    convenience init(length : Double) {        3.
        self.init(W : length, H : length)
    }
    convenience init() {        (4)
        self.init(width: 640.0, height: 940.0)
    }
}
var rectc1 = Rectangle(width : 320.0, height : 480.0)        5.
println(" A rectangle :\(rectc1.width) x \(rectc1.height)")
var rectc2 = Rectangle(W : 320.0, H : 480.0)        6.
println(" A rectangle :\(rectc2.width) x \(rectc2.height)")
var rectc3 = Rectangle(length: 500.0)        All landowners
println(" A rectangle 3:\(rectc3.width) x \(rectc3.height)")
var rectc4 = Rectangle()        today
println(" A rectangle 4:\(rectc4.width) x \(rectc4.height)")

Declare Rectangle as a class, where four constructors are also reloaded. The self.init statement is used in the constructor of lines and, and the convenience keyword is added before the constructor. convenience stands for convenience constructor, which means that we define the constructor as a horizontal proxy to invoke other constructors.
The self.init (W: length, H: length) statement is the constructor proxy defined in line for horizontal invocation, and the self.init (width: 640.0, height: 940.0) statement is the constructor proxy defined in line for horizontal invocation.

This is the end of the problem of reloading the constructor, and I hope that you can refer to the following examples to help you


Related articles: