eph*_*amd 2 java variables android var object
在实现用于Android的java库时,我遇到了严重的问题.我需要全局变量,我已经通过应用Singleton解决了这个问题.
但现在我需要使用变量而不指定类型.作为我发现使用Object o的解决方案.
对象o,我如何检查o的类型?
o.isArray () // Ok is type Array
但要知道它是int,double,......?
使用任何类型的Object或变量的另一种解决方案?
例如:
public String arrayToString (Object o) {
if (o.getClass (). isArray ()) {
Arrays.toString return ((Object []) o);
Else {}
o.toString return ();
}
}
Object [] a = (Object []) LIB.getConf ("test");
a_edit [0] = "new value";
a_edit [1] = 2013;
x.arrayToString ("test") / / return test
x.arrayToString (1989) / / return 1989
x.arrayToString (a) / / return [new value, 2013]
Run Code Online (Sandbox Code Playgroud)
谢谢,
使用instanceof运营商.
例如:
if (o instanceof Integer) {
//do something
} else if (o instanceof String) {
//do something else
} else if (o instanceof Object[]) {
//or do some other thing
} else if (o instanceof SomeCustomObject) {
//....
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9689 次 |
| 最近记录: |