泛型方法

-----------siwuxie095

   

   

   

   

   

   

泛型方法中可以定义泛型参数,此时参数的类型就是传入的数据类型

   

格式:

   

   

   

   

代码:

   

package com.siwuxie095.generic;

   

   

class GenX{

 

//泛型方法

public <T>T tell(T t){

return t;

}

 

}

   

public class GenericDemo06 {

   

public static void main(String[] args) {

GenX g=new GenX();

//String str=g.tell("siwuxie095");

System.out.println(g.tell("siwuxie095"));

//int i=g.tell(10);

System.out.println(g.tell(10));

}

   

}

   

   

运行一览:

   

   

   

   

   

   

【made by siwuxie095】

原文地址:https://www.cnblogs.com/siwuxie095/p/6577048.html