2012-05-16 79 views
0

我試過搜索周圍,我發現很多結果似乎是我需要的。但是我一直無法以對我有用的方式實施它。需要幫助=(實體類,反射,設置通用值

這是我等級:

import Apple; 
public static void SetAddChg(Apple a) throws ClassNotFoundException { 
    DateFormat addDate = new SimpleDateFormat("yyyy-MM-dd"); 
    DateFormat addTime = new SimpleDateFormat("HH:mm:ss"); 
    Date date = new Date(); 
    EntityManager entityManager = Persistence.createEntityManagerFactory("EntityLibraryPU").createEntityManager(); 
    entityManager.getTransaction().begin(); 
    a.setAddDate((addDate.format(date))); 
    entityManager.getTransaction().commit(); 
} 

做是因爲我特別聲明,該類不過,我想這是你可以通過任何類(蘋果,香蕉一個通用類?,橙),我該怎麼做

回答

0

對於要設置的公共屬性,你可以有BananaApple實現共同的接口(如「Audited」):

public interface Audited { 
    void setDateAdded(Date date); 
    //etc, etc 
} 

public class Apple implements Audited { /*...*/ } 

public class Banana implements Audited { /*...*/ } 

然後,你可以接受Audited你的helper方法:

public void setAuditFields(Audited auditedEntity) { 
    auditedEntity.setDateChanged(new Date()); 
    //...etc... 
} 

//... 
a.setSize(textboxSize.getText()); 
setAuditFields(a); 
+0

不幸的是我無法理解的是解決方案可言,但我沒有重寫我的問題=( –