泛型:自定义泛型方法
2015-12-16 10:21
211 查看
1、自定义泛型方法的格式
2、自定义泛型方法的代码
package com.atguigu.generic; /** * 自定义泛型方法 * @author LiPiaoShui */ public class User<T> { /** * 此自定义泛型方法使用了自定义泛型类中的泛型,不需要添加<T> */ public T getT(T t) { return t; } /** * 此自定义泛型方法没有使用泛型类中的泛型,需要添加<E> */ public <E> E getE(E e) { return e; } /** * 静态方法中可以使用非类的其他泛型 */ public static <E> void show(E e) { System.out.println(e); } // /** // * 以下方法会报编译错误,因为静态方法中不能使用类的泛型 // */ // public static void show(T t) { // System.out.println(t); // } // /** // * 不能在catch中使用泛型 // */ // public void tryFun() { // try {} catch(T t) {} // } }
3、测试自定义泛型方法的代码
//4.自定义泛型方法 @Test public void test4() { User<Boolean> user = new User<Boolean>(); Boolean flag = user.getT(true); //输出:true System.out.println(flag); int score = user.getE(90); //输出:90 System.out.println(score); }
4、静态方法中不能使用类的泛型
5、静态方法中可以使用其他的非类泛型
6、不能在catch中使用泛型
阅读更多
相关文章推荐
- 41_自定义泛型方法的练习与类型推断总结
- 泛型(2)——方法上自定义泛型
- java再复习——自定义泛型方法
- List<T>泛型数组API自带的Sort() 排序方法根据自定义排序实现
- java自定义泛型(泛形)方法
- 自定义泛型类型和方法
- java自定义泛型(泛形)方法
- 自定义泛型方法及其应用和类型参数的类型推断
- 自定义泛型方法及其应用
- 16.6 方法上自定义泛型
- Java学习笔记之泛型(二):在方法上自定义泛型
- 21扩展IEnumerable<T>泛型接口自定义LINQ的扩展方法
- 自定义泛型方法
- 泛型第一课,自定义泛型,泛型类,泛型接口,泛型的方法
- 117_容器_自定义泛型_深入1_子类_属性类型_重写方法类型_泛型擦除
- paip.自定义java 泛型类与泛型方法的实现总结
- java泛型学习-自定义泛型方法与类型推断总结
- Java泛型_自定义泛型方法
- 泛型集合 and 自定义泛型方法
- paip.自定义java 泛型类与泛型方法的实现总结