java泛型转换问题

java泛型转换问题,java,generics,casting,Java,Generics,Casting,我想实现一种传输对象模式。所以,我有一个方法可以通过BeanUtils.copyProperties(…,…)(apache common)填充对象的属性 代码如下: public abstract class BaseTO<TObject> { public Long id; /*** * Obtains id of the object that´s transfered * @return object´s id */

我想实现一种传输对象模式。所以,我有一个方法可以通过BeanUtils.copyProperties(…,…)(apache common)填充对象的属性

代码如下:

public abstract class BaseTO<TObject> {

    public Long id;

    /***
     * Obtains id of the object that´s transfered
     * @return object´s id
     */
    public Long getId() {
        return id;
    }

    /****
     * set transfered object´s id 
     * @param id object´s id
     */
    public void setId(Long id) {
        this.id = id;
    }

    /***
     * Fill transfer object properties.
     * @param entity entity to be transfered
     * @return self reference
     */
    public BaseTO<TObject> build(TObject entity){
        try {
            BeanUtils.copyProperties(this, entity);
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        customDataTransformation(entity);
        return this;
    }

    protected void customDataTransformation(TObject entity) {
    }


}
正如您在这里看到的,必须向(CustomerTO)转换。如果可能的话,我希望避免这种情况,使代码更简单

PublicBaseToBuild(ToObject实体)是否可能返回子类的对象

我希望澄清

提前谢谢。

也许可以试试这个:

    class BaseTO<TObject, R extends BaseTO<TObject,R>> {

    public R build(TObject entity) {
但更好的方法是简单地用TObject参数添加构造函数

public BaseTO(TObject entity) {
        try {
            BeanUtils.copyProperties(this, entity);
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        customDataTransformation(entity);
}
然后在每个扩展类中创建简单的构造函数

     public CustomerTO(Customer entity) {
        super(entity);
    }
忘记
build
方法,简单地使用它

CustomerTO toEdit = (customerId!=null ? new CustomerTO(entityObject):new CustomerTO());
这包括:

public class BaseTO<T> {

    public BaseTO<T> build(T entity) {
       return this;
    }

    public static class CustomerTO extends BaseTO<String> {

       @Override public CustomerTO build(String string) {
           return (CustomerTO) super.build(string);
       }
}
公共类BaseTO{
要构建的公共BaseT实体{
归还这个;
}
公共静态类CustomerTO扩展BaseTO{
@覆盖公共客户以生成(字符串){
返回(CustomerTO)super.build(字符串);
}
}
但是您必须为
BaseTO
的所有子类重写
build
。您只显式地编写一次强制转换,而不是每次调用
build


编辑:查看上面评论中@Paul提出的观点。你可能正遭受“给一个人一把锤子,对他来说一切都像钉子一样”的痛苦

你写这一切仅仅是因为你想跳过一次小的强制转换吗?@toto:我认为这个问题不仅仅是关于强制转换,而且也是关于编译时类型检查的问题。@toto.不,我会在很多地方使用它。我不是这方面的专家,但我不认为它是用来传输数据的,不是吗易于继承。看起来您需要一个对象工厂,它存储所有可能对象的属性,并根据需要生成子类,类型在运行时确定。好的ol'copy构造函数有什么问题?
CustomerTO-toEdit=new-CustomerTO(entityObject)
您将得到编译时检查和更简单的代码库维护。我认为您太聪明了。@Paul我正在尝试将业务逻辑与表示分离。我将使用构造函数更改代码。谢谢您的回答
public BaseTO(TObject entity) {
        try {
            BeanUtils.copyProperties(this, entity);
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        customDataTransformation(entity);
}
     public CustomerTO(Customer entity) {
        super(entity);
    }
CustomerTO toEdit = (customerId!=null ? new CustomerTO(entityObject):new CustomerTO());
public class BaseTO<T> {

    public BaseTO<T> build(T entity) {
       return this;
    }

    public static class CustomerTO extends BaseTO<String> {

       @Override public CustomerTO build(String string) {
           return (CustomerTO) super.build(string);
       }
}