如何访问超类?

时间:2015-04-03 07:19:56

标签: ios xcode swift

我正在使用继承并需要访问父变量 我怎样才能做到这一点?

class EFPaymentObject: NSObject {

    var price: NSString = "1200"
    var title: NSString!
    var desck: NSString!
    var hasPass: Bool!

    func getPrice() -> NSString() {
       return price
    }
}

class EFGoodsPayment: EFPaymentObject{

    var pay: NSString = "1"
    var type: NSString = "3"
    //var select_club_id: NSString = EFBuyBasket.sharedInstance.getClubId()
    var goods: NSString = ""
    var money = price      // doesn't work
    var money = getPrice() // doesn't work either
}

1 个答案:

答案 0 :(得分:3)

如果我保留你的例子,你可以在子类初始化期间访问超类的price属性,并设置money的默认值,如下所示:

class EFPaymentObject: NSObject {

    var price: NSString = "1200"
    var title: NSString?
    var desck: NSString?
    var hasPass: Bool?

}

class EFGoodsPayment: EFPaymentObject {

    var pay: NSString = "1"
    var type: NSString = "3"
    var goods: NSString = ""
    var money: NSString?

    override init() {
        super.init()
        money = super.price
    }

}


let payment = EFGoodsPayment()
println(payment.money!)       // prints "1200"