reflection mechanism

import java.lang.reflect.Field;

class Persion{
public static String name="张三";
private int age = 32;
protected String idno = "112233445566";

public Persion(){

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

public Persion(String name,int age,String idno){
this.name = name;
this.age = age;
this.idno = idno;
}

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;
}
public String getIdno() {
return idno;
}
public void setIdno(String idno) {
this.idno = idno;
}

}
public class Test {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException{
/* Get the class object Three ways*/
//1. Create a Persion object and a persistence object
//Note that there is a difference between the two. The persistence object exists in the heap area and the persistence object exists in the method area. In fact, it is the member variables, structures, and methods in the Persion class. Property
Persion persion = new Persion();
Class persion1 = persion.getClass();
// hashcode returns the physical address (hash code value) of object storage
System.out.println(persion1.hashCode());
System.out. println("persion1 = " + persion1.getName());
System.out.println("----------persion2-------------");
//2. Each class has a static class member variable, which can be assigned directly without creating
Class persion2 = Persion.class;
System.out.println(persion2.hashCode());
//The Persion object has been assigned to persistence2 Why can't persistence2 refer to static variables? ? ? ? ?
//System.out.println(persion2.name);
System.out.println(Persion.name); //Classes can directly reference static variables
//Create an instance object of a class to get the properties of the object by creating an instance
Object obj = persion2 .newInstance();
Field field =persion2.getField("name");
Object obj2 = field.get(obj);
System.out.println(obj2.toString());
System.out.println("persion2 = " + persion2.getName());
System.out.println("----------persion3-------------");
//3. Get
Class persion3 by reflection = Class.forName("com.lsw.forname.Persion");
Object obj3 = persion3.newInstance();
System.out.println(persion3.hashCode());
System.out.println("persion3 = " + persion3.getName());
//Get all member variables
/*Field[] fields = persion3.getDeclaredFields() ;
for(Field str:fields)
System.out.println(str + "|getName="+str.getName()+ "|value="+str.get(str.getName()));*/
// Exception in thread "main" java.lang.IllegalAccessException:
//Class com.lsw.forname.Test can not access a member of class
//com.lsw.forname.Persion with modifiers "private"
//Reflection is defined as follows: The AVA reflection mechanism is that in the running state, for any class, you can know all the properties and methods of the class; for any object, you can call any of its methods and properties;
//This dynamically obtained information and The function of dynamically calling the method of an object is called the reflection mechanism of the java language.
//But an error is reported to find the reason
field = session3.getDeclaredField("age");
field.setAccessible(true);
field.set(obj3, 32);
System.out.println(field.getName() + "=" + field.get(obj3));

/**
2134502363
persion1 = com.lsw.forname.Persion
----------persion2-------------
2134502363
张三
张三
persion2 = com.lsw.forname.Persion
----------persion3-------------
2134502363
persion3 = com.lsw.forname.Persion
age=32
*/
}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325127223&siteId=291194637