gpt4 book ai didi

ios - 如何访问父类(super class)?

转载 作者:搜寻专家 更新时间:2023-11-01 06:47:05 25 4
gpt4 key购买 nike

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

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
}

最佳答案

如果我保留你的示例,你可以在子类初始化期间访问父类(super class)的 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"

关于ios - 如何访问父类(super class)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29427950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com