java 通过泛型创建对象

java 通过泛型创建对象,第1张

java 通过泛型创建对象

百度一下啥也没有,先看看这个吧 

java 根据泛型创建对象,实例化泛型对象_Sirius的博客-CSDN博客_泛型创建对象实例化泛型对象在你发布的代码中,不可能创建一个泛型T,因为你不知道它是什么类型:public class Abc{ public T getInstanceOfT() { // There is no way to create an instance of T here // since we don...https://blog.csdn.net/qq_21460229/article/details/104351684

老实说我没看懂

换个思路,传入demo,或者class

    public static  void toIPageVo(IPage foIPage, IPageVo iPageVo,T voDemo) throws InvocationTargetException, IllegalAccessException {
        List foList = foIPage.getRecords();
        List toList = new ArrayList();
        for (Object foRecord : foList) {
            T vo=null;
            try {
               vo  = (T) voDemo.getClass().newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            BeanUtilImpl.copyToVo(vo,foRecord);
            toList.add(vo);
        }

        iPageVo.setRecords(toList);
        iPageVo.setCurrent(foIPage.getCurrent());
        iPageVo.setSize(foIPage.getSize());
        iPageVo.setTotal(foIPage.getTotal());
        iPageVo.setPages(foIPage.getPages());
    }
    public static  void toIPageVo(IPage foIPage, IPageVo iPageVo,Class clazz) throws InvocationTargetException, IllegalAccessException {
        List foList = foIPage.getRecords();
        List toList = new ArrayList();
        for (Object foRecord : foList) {
            T vo=null;
            try {
                vo  = (T) clazz.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            BeanUtilImpl.copyToVo(vo,foRecord);
            toList.add(vo);
        }

        iPageVo.setRecords(toList);
        iPageVo.setCurrent(foIPage.getCurrent());
        iPageVo.setSize(foIPage.getSize());
        iPageVo.setTotal(foIPage.getTotal());
        iPageVo.setPages(foIPage.getPages());
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5707491.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存