我有一个像这样的界面:

public interface BeanCreator<T> {
   public T createBean();
   public Class<T> getClazz();
}


我尝试自定义它:

public class CustomizedBeanCreator<T> implements BeanCreator<T>{

    @Override
    public T createBean() {
        CustomizedBean bean = new CustomizedBean();
        bean.setId(-2323);
        bean.setName("this bean is created by a customized creator");
        @SuppressWarnings("unchecked")
        T t = (T)bean;
        return t;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Class<T> getClazz() {
        return (Class<T>) CustomizedBean.class;
    }
}


由于这两个@SuppressWarnings,我感到不舒服。

我该怎么解决?

谢谢!

最佳答案

将签名(和实现)从

public class CustomizedBeanCreator<T> implements BeanCreator<T>{




public static class CustomizedBeanCreator implements BeanCreator<CustomizedBean> {

    @Override
    public CustomizedBean createBean() {
        CustomizedBean bean = new CustomizedBean();
        bean.setId(-2323);
        bean.setName("this bean is created by a customized creator");
        return bean;
    }

    @Override
    public Class<CustomizedBean> getClazz() {
        return CustomizedBean.class;
    }
}

10-04 11:03