Three common ways to reflect in Java

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.