我有這樣的代碼:如何初始化實例在Java泛型方法
public <V> V getPropertiesByObject(V sample) throws TimeoutException {
//settings fields using reflaction
return sample;
}
我稱之爲是這樣的:
MyClass a = getPropertiesByObject(new MyClass());
只是因爲我不知道如何構建一個實例除此以外。
我寧願:
public <V> V getPropertiesByObject(Class<V> sample) throws TimeoutException {
//how to create a new V instance?
return result;
}
有沒有辦法來修改我的原代碼?
我真的沒有看到'getPropertiesByObject(new MyClass());'的問題。 – Radiodef