2014-06-13 203 views
0

我可以在初始化程序中使用其他初始化程序嗎?在Swift的初始化程序中使用其他類初始化程序

class Car { 
    var manufacturer:String? 
    var speed:Int? 

    init (manufacturer manf:String){ 
     manufacturer = manf 
    } 

    init(manufacturer manf:String, speed spd:Int){ 
     manufacturer = manf // How can I use the other initializer here? 
     speed = spd 

    } 
} 


var b = Car(manufacturer: "bmw") 

var k = Car(manufacturer: "kia", speed: 30) 

回答

1

我想通了。我只是需要convenience之前我的中學init然後我可以使用self.init調用原init

class Car { 
    var manufacturer:String? 
    var speed:Int? 

    init (manufacturer manf:String){ 
     manufacturer = manf 
    } 

    convenience init(manufacturer manf:String, speed spd:Int){ 
     self.init(manufacturer: manf) 
     speed = spd 

    } 
} 
相關問題