2014-10-16 52 views
1

我在java中创建对象时出现问题,我有3个罐子,每个人都有一个名为“Person”的类,我将这些罐子文件 包含到我的项目中,需要定义3个对象的人,问题是:删除重复的代码,调用不同罐子中的相同对象

public class UtilClass { 
    public static com.jar1.Person definePerson1() { 
     com.jar1.Person person = new com.jar1.Person(); 
     person.setName(Constant.NAME); 
     person.setLastName(Constant.LASTNAME); 
     return person; 
    } 

    public static com.jar2.Person definePerson2() { 
     com.jar2.Person person = new com.jar2.Person(); 
     person.setName(Constant.NAME); 
     person.setLastName(Constant.LASTNAME); 
     return person; 
    } 

    public static com.jar3.Person definePerson3() { 
     com.jar3.Person person = new com.jar3.Person(); 
     person.setName(Constant.NAME); 
     person.setLastName(Constant.LASTNAME); 
     return person; 
    } 
} 

正如你所看到的,类是“相同”,但包装不同的是,我有这个UtilClass因为我在另一个类中定义的方法:

public void create() { 
    com.jar1.Group = new Group(UtilClass.definePerson1()); //Only accept com.jar1.Person 
    com.jar2.Group = new Group(UtilClass.definePerson2()); //Only accept com.jar2.Person 
    com.jar3.Group = new Group(UtilClass.definePerson3()); //Only accept com.jar3.Person 
} 

我怎样才能简化课堂UtilClass并避免重复的代码?我无法更改我的jar文件。

+0

请问您个人类共享公共接口? – SMA 2014-10-16 13:26:57

+0

丑,但你可以使用反射 – 2014-10-16 13:27:28

+0

谢谢Holger。这只是一个例子。真的,3个jar文件对应3个WS客户端,每个RequestType都有一个名为Person的属性(每个Person类对每个jar文件都是一样的),我需要用相同的值来设置它。感谢的人, – Candres 2014-10-16 13:39:21

回答

1

您可以使用反射来设置您的值。

例如,使用BeanUtilsConstructorUtils,这更容易比Java bean的API来使用(见answer

public static class UtilClass { 
    public static com.jar1.Person definePerson1() { 
     return newPerson(com.jar1.Person.class); 
    } 

    public static com.jar2.Person definePerson2() { 
     return newPerson(com.jar2.Person.class); 
    } 

    public static com.jar3.Person definePerson3() { 
     return newPerson(com.jar3.Person.class); 
    } 

    public static <T> T newPerson(Class<T> clazz) { 
     try { 
      T person = ConstructorUtils.invokeConstructor(clazz, null); 
      BeanUtils.setProperty(person, "name", Constant.NAME); 
      BeanUtils.setProperty(person, "lastName", Constant.LASTNAME); 
      return person; 
     } catch (Exception e) { 
      throw new RuntimeException(e); 
     } 
    } 
} 
0

如果三个Persos类没有用的名字和lastName字段或通用超与setName和setLastName方法的接口,最简单的解决方案是使用反射。

但是,使用反射是不好的做法。如果稍后将人名重命名为firstName,则代码将编译,并且没有任何内容会警告您,您的UtilClass已损坏。

1

如果这些类没有任何共同之处,即没有实现,你可以使用, 您可以使用标准java.beans包解决的任务,没有任何第三方库的通用接口:

import java.beans.Expression; 
import java.beans.Statement; 

public class UtilClass { 
    public static <T> T definePerson(Class<T> type) { 
    try { 
     Object o=new Expression(type, "new", null).getValue(); 
     new Statement(o, "setName", new Object[]{Constant.NAME}).execute(); 
     new Statement(o, "setLastName", new Object[]{Constant.LASTNAME}).execute(); 
     return type.cast(o); 
    } catch(Exception ex) { throw new IllegalStateException(ex); } 
    } 
} 

感谢泛型中,方法声明返回您传递的Class实例的类型。然后你的使用情况会是什么样子:

com.jar1.Group = new Group(UtilClass.definePerson(com.jar1.Person.class)); 
com.jar2.Group = new Group(UtilClass.definePerson(com.jar2.Person.class)); 
com.jar3.Group = new Group(UtilClass.definePerson(com.jar3.Person.class)); 
+0

,它的作品完美! – Candres 2014-10-17 13:01:33