Three common ways to reflect in Java
Package com.xiaohao.test;
public class test{
public static void Main (string[] args) throws ClassNotFoundException, Instantiationexception, illegalaccessexception {
Class<?> clazz=class.forname ("Com.xiaohao.test.User"); 1 Method One
User user= (user) clazz.newinstance ();
User user=user.class.newinstance (); 2 Method Two
User User2=new user (); 3 Method Three
User User=user2.getclass (). newinstance ();
User.setid (10);
User.setusername ("Xiao Hao");
User.setpassword ("123456");
SYSTEM.OUT.PRINTLN (user);
}
}
Package com.xiaohao.test;
Import java.util.ArrayList;
Import java.util.List;
public class User {
Private Integer ID;
Private String UserName;
private String password;
List<string> books=new arraylist<string> ();
Public Integer getId () {
return ID;
}
public void SetId (Integer id) {
This.id = ID;
}
Public String GetUserName () {
return userName;
}
public void Setusername (String userName) {
This.username = UserName;
}
Public String GetPassword () {
return password;
}
public void SetPassword (String password) {
This.password = password;
}
Public User (String userName, string password) {
Super ();
This.username = UserName;
This.password = password;
}
Public User () {
Super ();
}
Public list<string> Getbooks () {
return books;
}
public void Setbooks (List<string> books) {
This.books = books;
}
@Override
Public String toString () {
Return this.id+ "" +this.username+ "" +this.password+ "";
}
}
Package com.xiaohao.test;
Import Java.lang.reflect.Array;
Import Java.lang.reflect.Field;
Import Java.lang.reflect.Method;
public class test{
public static void Main (string[] args) throws Exception {
Class<?> clazz=class.forname ("Com.xiaohao.test.User"); 1 Method One
User user= (user) clazz.newinstance ();
User user=user.class.newinstance (); 2 Method Two
User User2=new user (); 3 Method Three
User User=user2.getclass (). newinstance ();
System.out.println ("The value of the User2 object is:" +user2);
System.out.println ("The name of the class is:" +user2.getclass (). GetName ());
Field Field=user2.getclass (). Getdeclaredfield ("number");
Field Field=user.class.getdeclaredfield ("number");
Field field=class.forname ("Com.xiaohao.test.User"). Getdeclaredfield ("number");
Field.setaccessible (TRUE);
Field.set (User2, "1000");
System.out.println ("The value of the User2 object is:" +user2);
Method Method=user.class.getdeclaredmethod ("Setusername", String.class);
Method.invoke (User2, "Little Hao Grandpa");
System.out.println ("The value of the User2 object is:" +user2);
Class<?> component=class.forname ("Com.xiaohao.test.User"). Getdeclaredfield ("Address"). Get (User2). GetClass (). Getcomponenttype ();
User.class.getDeclaredField ("Address"). Setaccessible (True);
int length= ((string[]) User.class.getDeclaredField ("Address"). Get (User2)). length;
System.out.println ("The length of the original array in the User2 is:" +length);
Object [] array= (object[]) array.newinstance (component, length+75);
System.out.println ("The length of the modified array in the User2 is:" +array.length);
User.setid (10);
User.setusername ("Xiao Hao");
User.setpassword ("123456");
SYSTEM.OUT.PRINTLN (user);
}
}
Three common ways to reflect in Java