Java 泛型
泛型类定义:
public class Test<T> { private T body; public T getBody() { return body; } public void setBody(T body) { this.body = body; } }
泛型方法定义:
public static <T>String getClassName(T t) { return t.getClass().getName(); }
测试:
public static void main(String[] args) { System.out.println(getClassName("1234")); System.out.println(getClassName(123)); System.out.println(getClassName(123D)); System.out.println(getClassName(123L)); System.out.println(getClassName(123F)); }
测试结果:
java.lang.String
java.lang.Integer
java.lang.Double
java.lang.Long
java.lang.Float