获得类类型的三种表达式 Class c1=Foo.class;
Classc2=foo1.getClass();
Classc3=Class.forName(“com.imooc.reflect.Foo”);
c1=c2=c3
通过类类型获得该类对象 FOOfoo=(Foo)c1.newInstance();//需要有无参构造方法
静态加载类 newFoo(); 编译时加载
动态加载类 Classc=Class.forName(“args[]”); 运行时加载
OfficeAbleoa=(OfficeAble)c.newInstance();
Oa.start();
基本类型和void关键字都存在类类型
想要得到类信息,先得到它的类类型,在通过各种getXX方法获得信息
方法类
Mehtod[] ms=c.getMethods() 获得所有public的函数,包括父类继承而来的
c.getDeclaredMethods() 获得所有自己声明的方法,可以是私有的,但没有从父类继承的
Class rerurnType=ms[i].getReturnType() 获得返回值类型的类类型(类.Class)
RerurnType.getName() 或者返回对象类的名称
Class[] paramTypes=ms[i].getParameterTypes() 获得参数列表的类类型
成员变量类
Field[] fs=c.getDeclaredFilds() 获得所有自己声明的成员变量,可以是私有的,但没有从父 类继承的
Field[] fs=c.getFilds()
Class fieldType=field.getType() 获得成员变量的类类型
fieldType .getName() 获得成员变量的类名称
构造函数类
Constructor[]cs=c.getDeclaredConstructors();构造函数一般都是获取自己声明的,不要继承的
Class[]paramTypes=cs[i].getParameterTypes()
方法的反射 method.invoke(对象,参数);