我需要幫助,這是Swift中的AnyObject類的示例。Android - AnyObject for Android like iOS Swift 2
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中,我無法做到這一點。那麼,是否有可能在Android中使用AnyObject類,就像在iOS中一樣?
這個問題還不夠清楚,你是什麼意思?你可能想編輯它... –
你想要[Object](https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html)類嗎? – kennytm