例Java反射我有數據層之後與克隆
public class DemoData implements Cloneable {
private String name;
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone(); //To change body of generated methods, choose Tools | Templates.
}
}
欲如下
public static void main(String[] args) {
DemoData demoData = new DemoData();
demoData.setName("Class Sources");
testReflectionDemo(demoData);
}
private static DemoData testReflectionDemo(DemoData demoData) {
try {
DemoData clone = (DemoData) demoData.clone();
clone.setName(demoData.getName());
clone.setValue(demoData.getValue());
return clone;
} catch (CloneNotSupportedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
我要分配的數據值(DemoData),以一個重複的數據(DemoData克隆)層轉換方法testReflectionDemo(demoData demoData)至方法testReflectionDemo(T T)反射,如圖below.I不知道如何繼續,請大家幫我
public <T> T testReflectionDemo(T t){
Class<?> aClass = t.getClass();
for (Method method : aClass.getMethods()) {
}
return null;
}
什麼clone.setName(demoData.getName的'點() ); clone.setValue(demoData.getValue());'?你知道克隆是如何工作的嗎? – shmosel
因爲語句正常運行,我認爲它可以工作,在實際的程序我已經包括以下功能 clone.setName(的getContent(demoData.getName())); –
那麼如果它運行?這並不是正確或必要的。 – shmosel