我需要帮助,这是 Swift 中 AnyObject 类的示例。
class OtherClass {
private var myValue: String?
func setValue(myValue: String) {
self.myValue = myValue
}
func getValue() -> String {
return myValue!
}
}
class MyClass {
init() {
let otherClass = OtherClass()
otherClass.setValue("Value here!")
let myValue = getValue(otherClass) as? String
print(myValue) // output is Value here!
}
func getValue(object: AnyObject) -> AnyObject {
let myObject = object as! OtherClass
return myObject.getValue()
}
}
我们可以将 OtherClass 对象添加到 getValue 方法的参数中。可以看到,参数方法是AnyObject而不是OtherClass。但在android中我不能这样做。那么,是否可以像在 iOS 中一样在 Android 中使用 AnyObject 类?