Learning __HTML5 of Java web-reflection classes

Source: Internet
Author: User
Tags reflection java web

This reflective class is in the NetEase cloud classroom 30 days easy to master javaweb inside learning. Can be written in frames. But why do you have to learn it. Of course it's for use. A class has several components, such as a member variable, a method, and a constructor method. Reflection even loads the class and dissect the various components of the class. in Java, there is an classes class that represents the byte code of a class. The class class has a forname () method that loads the bytecode of a class into memory and encapsulates it with a class object. The other two ways to get class objects are class names. class objects. GetClass () class classes common methods
Public constructor GetConstructor: reflects the (public) constructor of a class
Public constructor Getdeclaredconstructor: reflects a class's (private) constructor public Metod GetMethod: A (public) method that reflects a class
Public Metod Getdeclaremethod: reflects a class's (private) method public field GetField: reflects a class's (public) field, which is the property
Public field Getdeclaredfield: reflects the (private) field of a class, which is the property

Here is the case: Create a new Person class

Package com.test.reflect;
Import Java.io.InputStream;

Import java.util.List;
    public class Person {public String name = ' John ';
    private int password = 666;

    private static int age = 6;
    Public person () {System.out.println (' person ');
    Public person (String name) {System.out.println (name);
    Public person (String name, int password) {System.out.println (name + "" + password);
    Private person (List list) {System.out.println ("list");
    public void Run () {System.out.println ("Run");
    public void Run (String name, int password) {System.out.println (name + ":" + password);
    Class[] Run (String name, int[] password) {return new class[]{string.class};
    private void Run (InputStream in) {System.out.println (in);
    public static void run (int num) {System.out.println (num);
    public static void Main (string[] args) {    System.out.println ("main");

 }
}
Load class in Reflection, get 3 methods of byte Code of class
Package com.test.reflect;

public class Demo {public
    static void Main (string[] args) throws ClassNotFoundException {
        //1:
        Class clazz = Class.forName ("Com.test.reflect.Person");

        2:
        Class clazz1 = new Person (). GetClass ();

        3:
        Class clazz2 = person.class;
    }
Reflection constructors
Package com.test.reflect;
Import Java.lang.reflect.Constructor;
Import java.util.ArrayList;

Import java.util.List;

Import Org.junit.Test; public class Demo1 {//here, just look at the next @Test public person Test () throws Exception {class<?> cla
       ZZ = Class.forName ("Com.test.reflect.Person");
       Constructor<?> constructor = Clazz.getconstructor ();
       Person P = (person) constructor.newinstance ();
    return p; }//Reflection constructor: Public person () @Test public void Test1 () throws Exception {class<?> clazz = Class.
       forname ("Com.test.reflect.Person");
       Constructor<?> constructor = Clazz.getconstructor ();
       Person P = (person) constructor.newinstance ();
    System.out.println (P.name); }//Reflection constructor: Public person (String name) @Test public void Test2 () throws Exception {class<?> Claz
       z = Class.forName ("Com.test.reflect.Person"); Constructor<?> constructor = Clazz.getconstructor (STring.class);
       person p = [person] constructor.newinstance ("stars");
    System.out.println (P.name); }//Reflection constructor: Public person (String name, int password) @Test public void Test3 () throws Exception {Class
       <?> clazz = Class.forName ("Com.test.reflect.Person");
       Constructor<?> constructor = Clazz.getconstructor (String.class, Int.class);
       Person P = (person) constructor.newinstance ("Stars", 666);
    System.out.println (P.name); }//Reflection constructor: Public person (list list) @Test public void Test4 () throws Exception {class<?> Clazz
       = Class.forName ("Com.test.reflect.Person");
       Constructor<?> constructor = Clazz.getdeclaredconstructor (List.class);
       Constructor.setaccessible (TRUE);
       Person P = (person) constructor.newinstance (new ArrayList ());
    System.out.println (P.name); }//Reflection constructors Another method of note is only the parameterless constructor @Test public void Test5 () throws Exception {class<?> Clazz = Class.forName ("Com.test.reflect.Person");
       Person P = (person) clazz.newinstance ();
    System.out.print (P.name);
 }
}
Reflection method
Package com.test.reflect;
Import Java.io.FileInputStream;
Import Java.io.InputStream;

Import Java.lang.reflect.Method;

Import Org.junit.Test;
    public class Demo2 {//Demo1 demo1 = new Demo1 ();//person P = demo1.test ();
    Person p = new person (); Method of Reflection Class: public void Run () @Test public void Test1 () throws Exception {class<?> clazz = Class.forn
        Ame ("Com.test.reflect.Person");       
        Method method = Clazz.getmethod ("Run"); 
    Method.invoke (P); }//Reflection class method: public void Run (String name, int password) @Test public void Test2 () throws Exception {C
        Lass<?> clazz = Class.forName ("Com.test.reflect.Person");       
        Method method = Clazz.getmethod ("Run", String.class, Int.class); 
    Method.invoke (P, "ABCD", 6666);
       }//Reflection class Method: Public class[] Run (String name, int[] password) @Test public void Test3 () throws Exception { Class<?> clazz = Class.forName ("COM.TEST.REFLECT.PErson ");
       Method method = Clazz.getmethod ("Run", String.class, Int[].class);
       Class[] cs = (class[]) Method.invoke (p, "efgh", New int[]{1,2});
    System.out.println (Cs[0]); }//Reflection class method: private void Run (InputStream in) @Test public void Test4 () throws Exception {CLASS&LT;?&G T
       Clazz = Class.forName ("Com.test.reflect.Person");
       Method method = Clazz.getdeclaredmethod ("Run", Inputstream.class);
       Method.setaccessible (TRUE); Method.invoke (P, New FileInputStream ("D:\\1.txt"));
       1.txt a method must exist for the}//Reflection class: public static void run (int num) @Test public void Test5 () throws Exception {
       Class<?> clazz = Class.forName ("Com.test.reflect.Person");
Method method = Clazz.getmethod ("Run", Int.class); 
       Method.invoke (null, 666);  Method.invoke (P, 666); Both of these can be}//Reflection class methods: public static void run (int num) @Test public void Test6 () throws Exception {Cl Ass<?> clazz = Class. forname ("Com.test.reflect.Person");
Method method = Clazz.getmethod ("main", String[].class);  Method.invoke (NULL, New Object[]{new string[]{}}); Both can be Method.invoke (null, (Object) New string[]{});
 Null can be replaced with P object,}}
Reflection fields
Package com.test.reflect;

Import Java.lang.reflect.Field;

Import Org.junit.Test;
    Reflection field public class Demo3 {//Reflection field public String name = ' John ';
        @Test public void Test1 () throws Exception {person p = new person ();
        Class<?> clazz = Class.forName ("Com.test.reflect.Person");
        Field f = Clazz.getfield ("name");
        Gets the value of the field Object value = F.get (p);
        Gets the type of the field class<?> type = F.gettype ();
            if (Type.equals (String.class)) {//This is two object comparison value = (String) value;
        System.out.println (value);
        //Set the value of the field F.set (P, "stars");
    System.out.println (P.name);
    }//reflection field private int password = 666;
        @Test public void Test2 () throws Exception {person p = new person ();
        Class<?> clazz = Class.forName ("Com.test.reflect.Person");
        Field f = Clazz.getdeclaredfield ("password");
        F.setaccessible (TRUE); System.out.println (F.Get (p)); }//Reflection field private static int age = 6;
        As with non-static fields @Test public void Test3 () throws Exception {person p = new person ();
        Class<?> clazz = Class.forName ("Com.test.reflect.Person");
        Field f = Clazz.getdeclaredfield ("Age");
        F.setaccessible (TRUE);


    System.out.println (F.get (p));
 }

}

Write in the end: The purpose of writing this blog is to consolidate their knowledge, after all, look at the total than but their own hands to knock over the code to the truth, after the decision to learn a knowledge point to write a blog to consolidate the

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.