2021.6.30笔记 反射
作者:互联网
反射
概述
获取字节码Class对象的三种方式
Fields
public class Person {
public String name;
public int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
import java.lang.reflect.Field;
public class NewClass {
public static void main(String[] args) {
Class<Person> personClass = Person.class;
Field[] fields = personClass.getFields();
for (Field field : fields) {
System.out.println(field);
}
Field newField = personClass.getField("name");
Person per = new Person("小新",6);
Object o = newField.get(per);
System.out.println(o);
newField.set(per,"妮妮");
System.out.println(per);
}
}
getDeclaredFields
public class Person {
private String name;
public int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
import java.lang.reflect.Field;
public class NewClass {
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
Class<Person> personClass = Person.class;
Field[] fields = personClass.getDeclaredFields();
for (Field field : fields) {
System.out.println(field);
}
Field newField = personClass.getDeclaredField("name");
newField.setAccessible(true); //类中有private需要用setAcessible暴力反射
Person per = new Person("小新",6);
Object o = newField.get(per);
System.out.println(o);
newField.set(per,"妮妮");
System.out.println(per);
}
}
Constructor
public class Person {
public String name;
public int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class ConClass {
public static void main(String[] args) throws Exception {
Class personClass = Person.class;
Constructor constructor1 = personClass.getConstructor(String.class,int.class);
Object newPer = constructor1.newInstance("小新", 6);
System.out.println(newPer);
Object o = personClass.newInstance();
System.out.println(o);
}
}
Method
public class Person {
public void method(String name) {
System.out.println("我是方法" + name);
}
}
import java.lang.reflect.Method;
public class MethodClass {
public static void main(String[] args) throws Exception {
Class personClass = Person.class;
Method method1 = personClass.getMethod("method", String.class);
Person p1 = new Person();
method1.invoke(p1,"小新");
}
}
获取类名
Class personClass = Person.class;
String name = personClass.getName();
System.out.println(name);
标签:name,2021.6,age,30,笔记,class,Person,public,String 来源: https://blog.csdn.net/q4451728/article/details/118361344