/**
* 作者:
* 日期:2013-11-18
* 功能:泛型,java反射机制
*/
package com.ch;
import java.lang.reflect.Method;
public class Demo2 {
public static void main(String[] args) {
Gen<Bird> gen1 = new Gen<Bird>(new Bird());
gen1.showTypeName();
}
}
//
class Bird{
public void add(int a,int b){
System.out.println(a+b);
}
public void showType(){
System.out.println("aa");
}
}
//
class Gen<T>{
private T o;
//构造函数
public Gen(T a){
o = a;
}
//得到T的类型名称
public void showTypeName(){
System.out.println("类型名称时:"+o.getClass().getName());
//通过反射机制,我们可以得到T类型的很多信息
Method [] m = o.getClass().getDeclaredMethods();
//
for(int i=0;i<m.length;i++){
System.out.println(m[i].getName());
}
}
}
泛型
最新推荐文章于 2024-11-18 17:25:23 发布