Java reflect reflection Summary

Source: Internet
Author: User

--------------Testbeanpackage Lh.demo.bean;public class Testbean { private string username; private string Password; private string Age; public Testbean () { } public Testbean (string _username,string _ Password,string _age) {  this.username = _username;  This.password = _password;  This.age = _age; }& Nbsp;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 String getage () {  return age;& nbsp;}  public void Setage (string age) {  this.age = age; } public void print (String str) {  System.out. println ("Not static:" +str);  } private void Privateprint (String str) {  System.out.println ("Private:" + STR);  }  public static void Staticprint(String str) {  System.out.println ("Static:" +str);  } public String toString () {  System.out.println ( This.username+ "," +this.password+ "," +this.age ";  return this.username+", "+this.password+", "+THIS.AGE; } } --------------------------------
Package lh.demo.test;
Import Java.lang.reflect.constructor;import Java.lang.reflect.field;import Java.lang.reflect.method;import Lh.demo.bean.TestBean;
public class Reflect {//1 How do I get the instance object corresponding to each bytecode?//1) class.forname ("Full Path to Class") Recommended//Class<?> bean = Class.forName ("Lh.demo . Bean. Testbean ");//2) object. GetClass ()//class<?> bean = new Testbean (). GetClass ();//3) class name. class//class<?> bean = lh. Demo.bean.TestBean.class;
2 Create Instance object://Usual way: Testbean bean = new Testbean ("Lisi", "111", "22");//Reflection Mode://1) Known class name//Testbean Testbean = (Testbean) bean . newinstance ();//2) Unknown class name//Object object = Bean.newinstance ();  //3.Constructor Class (Java.lang.reflect)//Represents a constructor method in a class 1) Get the method of constructing a class://Instance of class. GetConstructors ()//class<?> bean = Class.forName ("Lh.demo.bean.TestBean");// Constructor[] con = bean.getconstructors ();//Object O1 = Con[0].newinstance ();//Method method1 = Bean.getmethod ("Tostri Ng ", null);//Method1.invoke (O1, NULL);//Object O2 = Con[1].newinstance (" Lisi "," 111 "," a ");//Method METHOD2 = Bean.getmethod ("toString", null);//Method2.invoke (O2, NULL);  //class instance. GetConstructor (class ... parmtypes)/ /class<?> Bean = Class.forName ("Lh.demo.bean.TestBean");//Constructor con = bean.getconstructor (String.class, String.class,string.class);//Object B = con.newinstance ("AAA", "111", "a");  // class instance. Getdeclaredconstructor (class ... parmtypes)//Private  //4.Field Class (Java.lang.reflect)//Represents classMember variable//1) to get the member variable of a class://Instance of class. Getdeclaredfields ()//class<?> bean = Class.forName ("Lh.demo.bean.TestBean") ;//Object o = bean.newinstance ();//field[] field = Bean.getdeclaredfields ();//For (Field f:fields) {//System.out.print ln (f.getname ());//}//class instance. GetFields ()//instance of class. Getdeclaredfield (variable name)//Class<?> bean = Class.forName ( "Lh.demo.bean.TestBean");//Object o = bean.newinstance ();//Field age = Bean.getdeclaredfield ("Age");// Age.setaccessible (TRUE);//Age.set (O, "");//System.out.println (Age.get (o));//instance of class. GetField (variable name)  //5. Method class//Represents a member of a class//1) Gets the member method of a class://Instance of class. Getdeclaredmethods ()//Gets inherited by all methods of this class (Public,protected,private), Instances of the//class of subclasses are not included. GetMethods ()//Gets all the methods including subclasses of the parent class (public)//instance of class. Getdeclaredmethod (method name, class ... Parameter type)//method = Bean.getdeclaredmethod ("Privateprint", string.class);//Method.setaccessible (TRUE); Private method  //An instance of the Class class. GetMethod (method name, class ... Parameter type)//method = Bean.getmethod ("Print", String.class);//difference://Public method[] GetMethods () returns all common methods of a class, including the public method of its inheriting class, and, of course, the method of the interface it implements. Public method[] The Getdeclaredmethods () object represents all of the methods declared by the class or interface, including common, protected, default (package) access, and private methods, but does not include inherited methods. Of course, it also includes the method of the interface it implements.
2) Method call://Reflection Way: Method.invoke (Object ... Parameter value)//////non-static Call method//Object O3 = Method.invoke (null, "AAA");/////static//Object O2 = Method.invoke (obj, "AAA");
public static void Main (string[] args) {try {} catch (Exception e) {e.printstacktrace (); } }}

Java reflect reflection Summary

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.