Reflection -- a must for advanced Java development

Keywords: Java

Introduction: reflection is a very important concept in java development. If you master the knowledge of reflection, you can better learn advanced Java courses, so you must learn - you know! This course mainly introduces the use of Class, the reflection of methods and member variables, and the understanding of the essence of set generics through reflection.

Use of class 1

1.1 use of class


package com.amoscxy;

public class ClassDemo1 {
    public static void main(String[] args) {
        Foo foo1 = new Foo();  // foo1 is an instance object of foo class
        // Foo is also an instance object. How to represent the instance object of Class
        // Any Class is an instance object of Class, which can be represented in three ways

        // The first representation -- > actually tells us that any class has an implicit static member variable class
        Class c1 = Foo.class;

        // The second expression is that the object of this class is known to pass the getClass method
        Class c2 = foo1.getClass(); // Class type of Foo class

        /*
         * Official website, C1 and C2 represent the class type of Foo class (class.type)
         * All things are objects
         * Class is also an object, an instance object of class
         * This object we call the class type of this class
         */

        // No matter c1 or c2 represents the Class type of Foo Class, a Class can only be an instance object of Class
        System.out.println(c1==c2);

        // The third way of expression
        Class c3 = null;
        try {
            c3 = Class.forName("com.amoscxy.Foo");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(c2==c3);

        // We can create an instance object of this class through its class type -- > create an instance of Foo through c1 or c2 or c3
        try {
            Foo foo = (Foo)c1.newInstance(); // A construction method with or without parameters is needed
            foo.print();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

class Foo{ // Foo is an object in itself, an object of java.long.Class
    void print() {
        System.out.println("foo");
    }
}

2 dynamic loading class

2.1 Java dynamic loading class

The classes in the function line should be loaded dynamically as much as possible. The dynamically loaded classes should be loaded at run time. It is not necessary to recompile once compiling

OfficeBetter.java

class OfficeBetter
{
    public static void main(String[] args) 
    {
        try{
            // Dynamically loading classes, loading at runtime
            Class c = Class.forName(args[0]);
            // Create objects of this class by class type
            OfficeAble oa = (OfficeAble)c.newInstance();
            oa.start();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

Word.java

class Word implements OfficeAble
{
    public void start(){
        System.out.println("Word start");
    }
}

OfficeAble.java - interface, is the standard

interface OfficeAble
{
    public void start();
}

3 obtaining method information

3.1 Java access method information

Posted by eerok on Fri, 01 May 2020 04:07:06 -0700