/*
*功能:演示反射机制
*
*/
package com.haitao.array;
import java.lang.reflect.Method;
public class Demo6_4 {
public static void main(String[] args) {
Gen<Bird> gen1=new Gen<Bird>(new Bird());
gen1.showTypeName();
}
}
class Bird
{
public void test1()
{
System.out.println("aaa");
}
public void count(int a,int b)
{
System.out.println(a+b);
}
}
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());
}
}
}
java中反射机制的演示
最新推荐文章于 2024-04-30 15:48:31 发布