1

java | 反射的运用

 1 year ago
source link: https://benpaodewoniu.github.io/2022/12/11/java73/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
  • 获取类的运行时结构
  • 动态创建对象执行方法

获取类的运行时结构

通过反射获取类的运行时完整结构

  • Field
  • Method
  • Constructor
  • Superclass
  • Interface
  • Annotation
package com.redisc;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

class Person {
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String name;

public Person(String name) {
this.name = name;
}
}


public class Test {


public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
Class person = Class.forName("com.redisc.Person");
// 获得类的名字
person.getName();
// 获得类的简单名字
person.getSimpleName();
// 获得类属性
Field[] fields = person.getFields(); // 只能找到 public 属性
fields = person.getDeclaredFields(); // 找到所有属性
Field name = person.getDeclaredField("name"); // 获取指定名称的属性
// 获得类的方法
person.getMethods(); // 获取 public 方法
person.getDeclaredMethods(); // 获取所有的方法
// 获得指定方法
Method getName = person.getMethod("getName", null);
Method setName = person.getMethod("setName", String.class); // 后面那个值就是参数的类型,有的话必须要写,因为 java 的重载机制
// 获得类指定构造器
Constructor[] constructors = person.getConstructors();
// 获得指定构造器
Constructor constructor = person.getDeclaredConstructor(String.class);

}
}

动态创建对象执行方法

package com.redisc;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

class Person {
public String getName() {
return name;
}

public String getName2() {
return name2;
}

public void setName(String name) {
this.name = name;
}

public String name;
private String name2;

public Person(String name) {
this.name = name;
}

public Person() {
}
}


public class Test {


public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class person = Class.forName("com.redisc.Person");

// 构造一个对象
Person person1 = (Person) person.getDeclaredConstructor().newInstance();
System.out.println(person1);

// 通过构造器创建对象
Person person2 = (Person) person.getDeclaredConstructor(String.class).newInstance("123");
System.out.println(person2.getName());

// 通过反射调用普通方法
Method setName = person.getDeclaredMethod("setName", String.class);
setName.invoke(person2, "333");
System.out.println(person2.getName());

// 通过反射操作属性
Field name = person.getDeclaredField("name");
name.set(person2, "555");
System.out.println(person2.getName());

// 通过反射操作私有属性
Field name2 = person.getDeclaredField("name2");
name2.setAccessible(true);
name2.set(person2,"777");
System.out.println(person2.getName2());
}
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK