代码如下,怎样在test函数里输出实际类型参数的名字,这个例子应该输出Double:public class T1<T> {
public static void main(String[] args) {
T1 t = new T1<Double>();
t.test();
}
public void test(){
System.out.println();
}
}
public static void main(String[] args) {
T1 t = new T1<Double>();
t.test();
}
public void test(){
System.out.println();
}
}
private T t;
public T getT() {
return t;
} public void setT(T t) {
this.t = t;
}
public T1(T t){
this.t = t;
}
public T1(){
}
public void test(){
if(t!=null){
System.out.println(t.getClass());
}
}
public static void main(String [] args){
T1 t = new T1<Double>(new Double(2.0));
t.test();
}
}
package com.pack.test;public class T1<T> { /**
* @param args
*/
private T value;
public void test(){
System.out.println(value.getClass());
}
public T getValue() {
return value;
}
public void setValue(T value){
this.value = value;
} public T1(T value){
this.value = value;
}
}
package com.pack.test;
import com.pack.test.T1;public class GenT { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
T1<Double> doubleT = new T1<Double>(new Double(88));
doubleT.test();
}}
不会实例化一个T类型的对象, value.getClass()要报错。
反射域http://topic.csdn.net/u/20090301/22/ca51e61f-9b25-4a08-b465-775353db5ffc.html