Java Basics Summary-Basic operations of reflection

Source: Internet
Author: User

I. The concept of reflection

The Java reflection mechanism is in the running state, for any class, can know all the properties and methods of this class, for any one object, can call any of its methods and properties; This dynamically acquired information and the ability to dynamically invoke the object's methods are called the reflection mechanisms of the Java language. To dissect a class, you must first obtain the bytecode file object for that class. The anatomy uses the method in class. So first get the object of class type corresponding to each bytecode file.
In layman's words, the Java Virtual machine will find the corresponding class file when loading the class, and generate a class object, each class object will only be generated once, even if it is the basic data type, there will be a class object, so that in Java everything is object, The introduction of a blogger's picture here, I think summed up very well



Photo from [71799078]


Two. Methods used in reflection
1. How to get a class object
package Test;public class ReflectTest {    public static void main(String[] args) throws Exception{        Student s1 = new Student();        Class stu1 = s1.getClass();        System.out.println(stu1.getName());                Class stu2 = Student.class;        System.out.println(stu1==stu2);        //最常用方法        Class stu3 = Class.forName("Test.Student");        System.out.println(stu1==stu3);    }}class Student{  }

The third method is generally used, the first one has the class instance, the second one needs to import the class package, the third forname path is the absolute path.

2. Get the method of the class and call
package Test;import java.lang.reflect.Method;public class ReflectTest {    public static void main(String[] args) throws Exception{        ReflectTest obj = new ReflectTest();        Method m1 = obj.getClass().getMethod("age",int.class);        m1.invoke(obj, new Integer(27));        m1.invoke(obj, 28);        Method m2 = obj.getClass().getMethod("age",Integer.class);        m2.invoke(obj, new Integer(27));        m2.invoke(obj,28);    }    public void age(int age) {        System.out.println("int age = " + age);    }    public void age(Integer age) {        System.out.println("Integer age = " + age);    }}

Output:
int age = 27
int age = 28
Integer Age = 27
Integer age = 28

3. A series of methods for obtaining class information

Take the singer class as an example

  Package Test;public class Singer {public String name;    public String sex;    Private String Song;        Public Singer () {} public Singer (string name, String sex, string song) {this.name = name;        This.sex = sex;    This.song = song;    } public void SetName (String name) {this.name = name;    public void Setsex (String sex) {this.sex = sex;    } public void Setsong (String song) {this.song = song;    } public String GetName () {return name;    } public String Getsex () {return sex;    } public String Getsong () {return song; }}
package Test;import java.lang.reflect.*;public class ReflectTest1 {    public static void main(String[] args) throws Exception{        Class clazz = Class.forName("Test.Singer");        //获得所有方法    Method[] methods = clazz.getDeclaredMethods();    for (Method m: methods) {        System.out.println(m);    }        //获取所有成员属性变量    Field[] fields = clazz.getDeclaredFields();    for (Field f: fields) {        System.out.println(f);    }        //获取所有构造方法    Constructor[] constructors = clazz.getDeclaredConstructors();    for (Constructor c: constructors) {        System.out.println(c);    }  }}

Output:

    public java.lang.String Test.Singer.getName()    public void Test.Singer.setName(java.lang.String)    public java.lang.String Test.Singer.getSex()    public java.lang.String Test.Singer.getSong()    public void Test.Singer.setSex(java.lang.String)    public void Test.Singer.setSong(java.lang.String)    public java.lang.String Test.Singer.name    public java.lang.String Test.Singer.sex    private java.lang.String Test.Singer.song    public Test.Singer()    public Test.Singer(java.lang.String,java.lang.String,java.lang.String)


Three. Summary
    • Get the Class object
    • Method of calling Class
    • Using the reflection API to manipulate information

Java Basics Summary-Basic operations of reflection

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.