我被困住了。 我尝试制作正确的代码架构师,但我无法解决以下情况: 我有一些基类,并从它继承类:
class BaseClass {
var name: String
init(name: String) {
self.name = name
}
}
class ChildClass: BaseClass {
var otherName: String
init(otherName: String) {
self.otherName = otherName
}
}
以下功能:
func someFunc() -> BaseClass {
let a = ChildClass.init(otherName: "NAME")
return a
}
如何获取参数“otherName”? 可用参数只有一个 - “名称”,但我想得到“otherName”:
let b = someFunc()
b.PARAMETERS
答案 0 :(得分:0)
您可以使用:
(b as! ChildClass).otherName
您的函数将对象作为BaseClass
对象返回,因此您无法访问任何ChildClass
方法。要访问ChildClass
方法,您需要使用ChildClass
将对象转换为as!
,因为我们知道它是ChildClass
对象。之后,您将能够访问ChildClass
的所有参数。