我有以下代码:
protected <T> T getValueForKey(String key) {
T value = null;
// currentStats is just a Bundle
if (currentStats.containsKey(key)) {
return value;
}
return value;
}
Run Code Online (Sandbox Code Playgroud)
我该如何设置限制?例如,T是String或int或double.这可能吗?
PS
我不想用
protected <T extends String> T getValueForKey(String key) {
}
Run Code Online (Sandbox Code Playgroud)
因为我不想只有弦乐..
由于您不能重载返回值,我建议使用不同的访问器方法:
protected int getIntForKey(String key);
protected double getDoubleForKey(String key);
protected String getStringForKey(String key);
Run Code Online (Sandbox Code Playgroud)