混淆了这个初始化方法接受这些类型的参数是什么?

时间:2015-09-01 18:11:42

标签: swift class initialization

//对不起,这可能有点模糊,但我无法弄清楚为什么super.init有2个类型长度参数???也许这太模糊但我很困惑?

init(length: Int) {

super.init(length: length, width: length) 


}

1 个答案:

答案 0 :(得分:1)

您正在呼叫super初始化一个正方形(可以这么说),其中length = width = the value you pass into the subclass's init

最好将super.init标签更改为height:width:以避免这些混淆。

修改

super.init(length: length, width: length)
            |        |      |       |
            |        |      |      Another parameter's value
            |        |     Another parameter's name
            |   This is the parameter's value, not type
        This is a parameter's name    

'type'仅在声明函数时适用:

class SuperClass {
    init(length: Int, width: Int) {
        // do something
    }
}