获取Class类对象
static Class forName(String className);
通过类名调用。是一个静态方法。传入参数是一个完整的包名.类名
例如:
Demo1====>com.qfedu.a.Demo1
Class 类名.class
通过制定的类名调取。获取类所对应的属性
Class 类对象.getClass();
通过类对象获取当前类对象Class
package com.qfedu.c_reflect;
public class GetClassObject {
public static void main(String[] args) throws ClassNotFoundException {
//例如, String.class的类型是Class<String> 。 如果正在建模的类是未知的,请使用Class<?> 。
Class<?> cls1 = Class.forName("com.qfedu.c_reflect.Peroson");//一般都使用股这个
Class<Peroson> cls2 = Peroson.class;
Class<? extends Peroson> cls3 = new Peroson().getClass();
System.out.println(cls1 == cls2);
System.out.println(cls1 == cls3);
System.out.println(cls2 == cls3);
}
}
获取构造方法类对象
Constauctors[] getConstructors();
获取当前类中所有非私有化的构造方法。返回值是一个数组类型的数据
Constructors[] getDeclaredConstructors();
获取当前类中所有的构造方法。返回值是一个数组类型的数据
Constructor getConstuctor(Class …initParameterType);
根据指定的参数,返回一个非私有化的构造方法
Constructor getDeclaredConstuctor(Class … initParameterType);
package com.qfedu.c_reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class GetConstructor {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class<?> cls1 = Class.forName("com.qfedu.c_reflect.Peroson");
Constructor<?>[] constructors = cls1.getConstructors();//非私有化的构造方法
for (Constructor<?> constructor : constructors) {
System.out.println(constructor);
}
System.out.println("=========");
Constructor<?>[] declaredConstructors = cls1.getDeclaredConstructors();
for (Constructor<?> declaredConstructor : declaredConstructors) {
System.out.println(declaredConstructor);
}
System.out.println("=========>");
Constructor<?> constructor = cls1.getConstructor(null);
System.out.println(constructor);
System.out.println("========");
Constructor<?> constructor1 = cls1.getConstructor(int.class);
System.out.println(constructor1);
System.out.println("=======");
Constructor<?> declaredConstructor = cls1.getDeclaredConstructor(int.class, String.class);
System.out.println(declaredConstructor);
//实例化对象
Object obj1 = constructor.newInstance(null);
System.out.println("obj1"+obj1);
Object obj2 = constructor1.newInstance(10);
System.out.println("obj2"+obj2);
//私有化的不能使用这种方法进行赋值
Object obj3 = declaredConstructor.newInstance(1, "老邢");
System.out.println("obj3"+obj3);
}
}
获取Method类对象
Method[] getMethods();
获取非私有化的成员方法。包括了从父类继承而来可以在子类中使用的成员方法。
Method[] getDeclaredMethods
获取所有的方法。但是不包含从父类继承的方法Method getMethod(String name, Object … obj);
获取一个方法,通过方法的名字获取的
/*
* Object invoke(Object obj, Object …args); 重中之重
* 参数 obj Person类对象
* 参数 args 执行当方法需要传入实际的参数
* */
//Person person = new Person();
//person.game()
//game====>game()对应好的
//参数 obj 就是 perosn
game.invoke(cls.getConstructor(null).newInstance(null), null);
game1.invoke(cls.getConstructor(null).newInstance(null), “骚磊”);
package com.qfedu.c_reflect;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class GetMethodObject {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class<?> cls = Class.forName("com.qfedu.c_reflect.Peroson");
Method[] methods = cls.getMethods();
for (Method method : methods) {
System.out.println(method);
}
System.out.println("========");
//getDeclaredMethods 获取所有的方法。但是不包含从父类继承的方法
Method[] declaredMethods = cls.getDeclaredMethods();
for (Method declaredMethod : declaredMethods) {
System.out.println(declaredMethod);
}
System.out.println("===========>");
//获取指定某一个方法
//Method getMethod(String methodName,Class....initParameterType)
Method game = cls.getMethod("game", null);
System.out.println(game);
System.out.println("=========>");
Method game1 = cls.getMethod("game", String.class);
System.out.println(game1);
System.out.println("========");
//获取私有的方法
Method testPrivate = cls.getDeclaredMethod("testPrivate", null);
System.out.println(testPrivate);