如何从我的子类访问属性?
How can I access properties from my subclasses?
我在从我的子 class 访问属性时遇到问题。我定义了 superclass,然后创建了继承自 superclass 的 subclasses。我在 subclasses 中初始化了 superclass。
我有一个 class 叫做 Box。在这个 class 中,我有一个名为 Offer 的 属性,其类型为 OfferType。
var 报价:报价类型?
我创建了 class OfferType 并在其中设置了一些属性。
class OfferType {
var Name: String?
var Image: UIImage?
var Description: String?
init(Name: String, Image: UIImage, Description: String) {
self.Name = Name
self.Image = Image
self.Description = Description
}
}
比起我创建了 OfferType
的 3 个子class
class Discount: OfferType {
var Quantity: Int?
init() {
super.init(Name: "Discount", Image: UIImage(named: "Discount")!, Description: "nice")
}
}
class FreeShipping: OfferType {
var From: String?
var To: String?
init() {
super.init(Name: "FreeShiping", Image: UIImage(named: "FreeShipping")!, Description: "lalaa")
}
}
class BoGoF: OfferType {
lazy var SecondProduct = String()
init() {
super.init(Name: "BoGoF", Image: UIImage(named: "BoGoF")!, Description: "boh")
}
}
现在,在将子class 分配为属性 Offer inside BOX class 之后,我正在尝试获取子class 的属性。
我做不到,Xcode 给我的唯一选择是访问 superclass (OfferType )
的属性
我需要获得 属性 数量的子class 折扣。
这是我尝试访问属性的地方
首先我将 subclass 分配给 class Box
中的 属性
newBox.Offer = 折扣
现在我正在尝试获取折扣的属性,但它没有给我
newBox.Offer。
我试过施法但是没用
我的 class newbox 是这样定义的
这就是 class 框的定义方式
您的 newBox
实例可能具有 offer
属性 定义为 OfferType
.
即使您将 Discount
的实例放入 offer
属性 中,编译器也无法知道,因为您告诉编译器的只是 offer
将包含一个 OfferType
。它无法知道将包含在那里的 specific OfferType
。
您必须将 offer
转换为您期望的类型。我建议您查看 as
和 is
。这是一个例子:
if let discount = newBox.offer as? Discount {
print("Discount Quantity is \(discount.quantity)")
}
这里的 as?
关键字基本上告诉编译器以下内容:
is newBox.offer
a kind of Discount
? If so return that, otherwise,
return nil.
我在从我的子 class 访问属性时遇到问题。我定义了 superclass,然后创建了继承自 superclass 的 subclasses。我在 subclasses 中初始化了 superclass。
我有一个 class 叫做 Box。在这个 class 中,我有一个名为 Offer 的 属性,其类型为 OfferType。
var 报价:报价类型?
我创建了 class OfferType 并在其中设置了一些属性。
class OfferType {
var Name: String?
var Image: UIImage?
var Description: String?
init(Name: String, Image: UIImage, Description: String) {
self.Name = Name
self.Image = Image
self.Description = Description
}
}
比起我创建了 OfferType
的 3 个子classclass Discount: OfferType {
var Quantity: Int?
init() {
super.init(Name: "Discount", Image: UIImage(named: "Discount")!, Description: "nice")
}
}
class FreeShipping: OfferType {
var From: String?
var To: String?
init() {
super.init(Name: "FreeShiping", Image: UIImage(named: "FreeShipping")!, Description: "lalaa")
}
}
class BoGoF: OfferType {
lazy var SecondProduct = String()
init() {
super.init(Name: "BoGoF", Image: UIImage(named: "BoGoF")!, Description: "boh")
}
}
现在,在将子class 分配为属性 Offer inside BOX class 之后,我正在尝试获取子class 的属性。
我做不到,Xcode 给我的唯一选择是访问 superclass (OfferType )
的属性我需要获得 属性 数量的子class 折扣。
这是我尝试访问属性的地方
首先我将 subclass 分配给 class Box
中的 属性newBox.Offer = 折扣
现在我正在尝试获取折扣的属性,但它没有给我
newBox.Offer。
我试过施法但是没用
我的 class newbox 是这样定义的
这就是 class 框的定义方式
您的 newBox
实例可能具有 offer
属性 定义为 OfferType
.
即使您将 Discount
的实例放入 offer
属性 中,编译器也无法知道,因为您告诉编译器的只是 offer
将包含一个 OfferType
。它无法知道将包含在那里的 specific OfferType
。
您必须将 offer
转换为您期望的类型。我建议您查看 as
和 is
。这是一个例子:
if let discount = newBox.offer as? Discount {
print("Discount Quantity is \(discount.quantity)")
}
这里的 as?
关键字基本上告诉编译器以下内容:
is
newBox.offer
a kind ofDiscount
? If so return that, otherwise, return nil.