3

java泛型 方法返回值带有泛型

 3 years ago
source link: http://www.blogjava.net/paulwong/archive/2021/03/19/435828.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
java泛型 方法返回值带有泛型

My Links

Blog Stats

  • Posts - 1061
  • Stories - 10
  • Comments - 108
  • Trackbacks - 0

留言簿(42)

随笔分类(1221)

随笔档案(1016)

文章分类(7)

文章档案(10)

收藏夹(2)

Develop

E-BOOK

Other

阅读排行榜

评论排行榜

60天内阅读排行

java泛型 方法返回值带有泛型

        /**
         * 
         * @param <T>声明此方法持有一个类型T,也可以理解为声明此方法为泛型方法
         * @param clazz 作用是指明泛型T的具体类型
         * @return 指明该方法的返回值为类型T
         * @throws InstantiationException
         * @throws IllegalAccessException
         */
        public <T> T getObject(Class<T> clazz) throws InstantiationException, IllegalAccessException {
            T t = clazz.newInstance();//创建对象
            return t;
        }

方法返回值前的<T>的左右是告诉编译器,当前的方法的值传入类型可以和类初始化的泛型类不同,也就是该方法的泛型类可以自定义,不需要跟类初始化的泛型类相同

posted on 2021-03-19 15:10 paulwong 阅读(51) 评论(0)  编辑  收藏 所属分类: J2SE

Powered by:
BlogJava
Copyright © paulwong


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK