泛型方法:
package Dey01;
/**
* 泛型方法
* 语法:<T>返回值类型
*/
public class MyGenericMethod {
//作为方法的参数,或者是返回值
//1.作为参数
public <T> void show(T t){
System.out.println("泛型方法:"+t);
}
//2.作为返回值
public <T> T show1(T t){
System.out.println("泛型方法:"+t);
return t;
}
}
测试类:
//--------------泛型方法-------------
//泛型方法T的类型是由我们传递的参数类型来决定的...
MyGenericMethod myGenericMethod = new MyGenericMethod();
myGenericMethod.show("123!!!");
myGenericMethod.show1("666!!!!!!");
测试结果:
泛型方法:123!!!
泛型方法:666!!!!!!