JAVA--反射机制

反射的概念

 反射的概念:

JAVA反射机制是在运行状态中

    对于任意一个类,都能够知道这个类的属性和方法

    对于任意一个对象,都能够调用他的任意的一个方法

反射的使用场景:

    逆向代码,例如反编译

    与注释相结合的框架

    单纯的反射机制应用框架

    动态生成类框架

Java的反射机制是由Class类和java.lang.reflect包下的Method、Field、Constructor等类支持的。

File:

先创建一个对象里面定义好他的属性,方法,构造方法

File获取对象的三种方法:

      先创建一个对象

    Student student = new Student();

  第一种 通过对象名.getClass()方法获取
    Class<?> class1 = student.getClass();
  第二种 通过类名.class方式获取
    Class<?> class2 = Student.class;
 第三种 通过Class.forName("xxx")方法获取(存储的是类的完整路径名所对应的对选哪个的所有属性和方法信息)
Class<?> class3 = Class.forName("org.wangyukai.model.Student");
 
public static void main(String[] args) {
// 创建Student对象
Student student = new Student();
// Java反射机制获取Class对象的三种方法
// 第一种 通过对象名.getClass()方法获取
Class stuClass1 = student.getClass();
System.out.println("stuClass1 is "+stuClass1.getName());

// 第二种 通过类名.class方式获取
Class stuClass2 = Student.class;
System.out.println("stuClass2 is "+stuClass2.getName());
System.out.println(stuClass1 == stuClass2);

// 第三种 通过Class.forName("xxx")方法获取
Class stuClass3 = null;
try {
stuClass3 = Class.forName("org.fuxian.model.Student");
} catch (ClassNotFoundException e) {
System.out.println("找不到这个类");
}

反辅助类-->Method(方法类)

Method[] getMethods()

获取全部的public的函数(包括从父类继承的、从接口实现的所有public函数)

Method[] getDeclaredMethods()

获取全部的类自身声明的函数,包含public、protected和private方法

Method getMethod(String name)

获取"名称是name,参数是parameterTypes"的public的函数(包括从父类继承的、从接口实现的所有public函数)

Method getDeclaredMethod(String name)

获取"名称是name,参数是parameterTypes",并且是类自身声明的函数,包含public、protected和private方法

package org.wangyukai.test;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
/*------Method --方法类
* 1.(获取公共方法) 获取返回值类型:getReturnType(),getType();
* 返回类型的简称:getSimpleName()
* 获取参数:getParameters(数组类型)----使用fore循环输出
*/
public class TestMethod {
//1:
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException {
Class<?> class3 = Class.forName("org.wangyukai.model.Student");
//1.获取通过反射得到的Class对象所有的public方法(包括继承 接口 )获取全部的public的函数函数)

Method[] publicMethods = class3.getMethods();

for (Method method : publicMethods) {
System.out.print("public "+method.getReturnType().getSimpleName()+" "+method.getName()+"(");
// System.out.println(method.getName());
System.out.println(method);
Parameter [] ps = method.getParameters();
int count = 0;
for (Parameter p : ps) {
System.out.print(p.getType().getSimpleName()+" "+p.getName());
if(count<ps.length-1) {
System.out.println(",");
}
count++;
}
System.out.println(")");
}
System.out.println("-------------------------------------");
//2:获取全部的类自身声明的方法,包含public、protected和private方法

Method[] allMethods = class3.getDeclaredMethods();
for (Method method : allMethods) {
System.out.println(method.getName());
}
System.out.println("-----------------------------------------");
//3:根据方法名称和参数获取方法public方法
//括号里面是方法名称和类型(参数).class。如果多的直接在后面加就行
Method publicMethod = class3.getMethod("say",String.class);
// Method publicMethod = class3.getMethod("eat");
System.out.println(publicMethod.getName());

System.out.println("-----------------------------------------");
//4.根据方法名称和参数获取方法 类自身声明的所有方法
// Method allMethod = class3.getDeclaredMethod("say",String.class);
Method allMethod = class3.getDeclaredMethod("eat");
System.out.println(allMethod.getName());
System.out.println(allMethod);

}

反辅助类--->Field类(属性)

-----Field()----属性(三大权限)
* getFields() //1.获取所有公共的属性 (权限不限)
* getDeclaredFields() //2.获取所有的属性
* getField("参数") //3.根据参数获取指定的公共的属性
* getDeclaredField("参数"); //4.根据参数获取指定的所有的属性 (权限不限)

 

public class TestFile {

public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException {
Class<?> class3 = Class.forName("org.wangyukai.model.Student");

//1.获取所有公共的属性
Field[] publicFiles = class3.getFields();
for (Field field : publicFiles) {
System.out.println(field);
}
System.out.println("------------");
//2.获取所有的属性
Field[] allFiles = class3.getDeclaredFields();
for (Field field : allFiles) {
System.out.println(field);
}

System.out.println("------------");

//3.根据参数获取指定的公共的属性
Field publicField = class3.getField("sex");
System.out.println(publicField);

System.out.println("------------");
//4.根据参数获取指定的所有的属性
Field allField = class3.getDeclaredField("sex");
System.out.println(allField);
}

反辅助类---->Constructor(构造方法类)

---------Constructor()-------构造方法
* getConstructors() //1.获取所有的公共的构造方法
* getDeclaredConstructors() //2.获取所有的构造方法(权限不限)
* .getConstructor(int.class,String.class) //3.根据参数类型及个数获取指定的public构造方法
* getDeclaredConstructor(int.class) //4.根据参数类型及个数获取指定的所有的构造方法 (权限不限)

public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException {


Class<?> class3 = Class.forName("org.wangyukai.model.Student");
//1.获取所有的公共的构造方法
Constructor<?> [] publicConstructors = class3.getConstructors();
for (Constructor<?> constructor : publicConstructors) {
System.out.println(constructor);
}
System.out.println("---------------------");
//2.获取所有的构造方法
Constructor<?> [] allConstructor = class3.getDeclaredConstructors();
for (Constructor<?> constructor : allConstructor) {
System.out.println(constructor);
}
System.out.println("---------------------");
//3.根据参数类型及个数获取指定的public构造方法
Constructor<?> publicConstructor = class3.getConstructor(int.class,String.class);
System.out.println(publicConstructor);


System.out.println("---------------------");
//4.根据参数类型及个数获取指定的所有的构造方法
Constructor<?> constructor = class3.getDeclaredConstructor(int.class);
System.out.println(constructor);
}

反射的简单应用------>invock

第一种方式;

public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {


Class<?> class3 = Class.forName("org.wangyukai.model.Student");

//调用一下Student类中的run方法
String MethodName = "run";
Method runMethod = class3.getMethod(MethodName);

//调用setName() 给对象赋值
String setNameMethodName = "setName";
Method setNameMethod = class3.getMethod(setNameMethodName, String.class);


//调用这个runMethod方法
//需要创建一个对象

Object obj = class3.newInstance();

setNameMethod.invoke(obj, "王渝凯");
//调用方法(1.在哪个对象上调用,2.方法需要的参数...)
runMethod.invoke(obj);

}

 第二种方式:

public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

String className = "org.wangyukai.model.Student";//路径
String methodName = "run";//调用的方法名


Class<?> class3 = Class.forName(className);//调用的路径
Method runMethod = class3.getMethod(methodName);//调用的方法名称

//创建一个对象
Object obj = class3.newInstance();

//调用方法
runMethod.invoke(obj,"wangyukai ");
}

 

posted @ 2020-07-31 21:50  大可耐啊  阅读(32)  评论(0)    收藏  举报