51-Understanding the reflection mechanism

Cognitive reflection mechanism

  The reason why there is so much open source technology support in the Java language, a large part of it comes from the biggest feature of Java-the reflection mechanism. If you can't flexibly use the reflection mechanism for project development and design now, it can be said that you have not touched the essence of Java.
  All technologies achieve only one goal:Reusability

Introduction to reflection mechanism

  The first consideration for reflection technology is the concept of negative and positive . The so-called "positive" operation means that when we want to use a class, we must first import the package where the program is located, and then instantiate the object according to the class, and rely on the object to call the method in the class. But if you say "reverse", infer its type based on the instantiated object.
Forward operation

import java.util.Date;		//导入程序所在的包,知道对象的出处

public class test {
    
    

	public static void main(String[] args) {
    
    
		Date date = new Date();		//通过类产生实例化对象
		System.out.println(date.getTime());		//使用方法
	}
}

  In order to realize the reverse processing operation, the first thing to be adopted is an operation method provided in the Object class:

  • Get Class information:public final Class<?> getClass();

Observe the use of Class objects

package java_study_aliyun.Basic;

import java.util.Date;		//导入程序所在的包,知道对象的出处

public class test {
    
    

	public static void main(String[] args) {
    
    
		Date date = new Date();		//通过类产生实例化对象
		System.out.println(date.getClass());		//通过实例化对象找到对象的所属类型
	}
}

  getClass() can help users find the root of the object.

Three instantiation modes of Class objects

  All core operations in reflection are expanded through Class class objects. It can be said that Class class is the source of reflection operations, but if this class wants to obtain its instantiated objects, it can be done in three ways. Observe java. The definition of the lang.Class class: public final class Class<T> extends Object implements Serializable,GenericDeclaration,Type,AnnotatedElement;
  Since JDK1.5, the Class class can be marked with generics when it is defined. Such usage is mainly to avoid the so-called downward transformation. Explain three forms of instantiation through specific operations.

  1. [Object class support] Object class can obtain Class objects based on instantiated objects:public final Class<?> getClass();
package java_study_aliyun.Basic;

import java.util.Date;		//导入程序所在的包,知道对象的出处

class Person{
    
    }

public class test {
    
    

	public static void main(String[] args) {
    
    
		Person per = new Person();
		Class<? extends Person> cls  = per.getClass();
		System.out.println(cls);
		System.out.println(cls.getName());		//获取类的完整名称
	}
}
  • This method has a shortcoming that is not a shortcoming: if you just want to obtain a Class object, you must generate a specified class object before you can obtain it.
  1. [JVM Direct Support] Adopt "Class.class”Is instantiated directly
package java_study_aliyun.Basic;

import java.util.Date;		//导入程序所在的包,知道对象的出处

class Person{
    
    }

public class test {
    
    

	public static void main(String[] args) {
    
    
		Class<? extends Person> cls  = Person.class;
		System.out.println(cls);
		System.out.println(cls.getName());		//获取类的完整名称
	}
}
  • If you want to use this mode, you must import the development package corresponding to the program.
  1. [Class support] Provide a static method in the Class class:public static Class<?> forName(String className) throws ClassNotFoundException
package java_study_aliyun.Basic;

public class test {
    
    

	public static void main(String[] args) throws ClassNotFoundException {
    
    
		Class<?> cls = Class.forName("java.util.Date");
		System.out.println(cls);
		System.out.println(cls.getName());		//获取类的完整名称
	}
}
  • The biggest feature of this mode is that the type to be used is defined directly in the form of a string, and there is no need to write import statements in the program.
  • If the program class to be used does not exist at this time, "java.lang.ClassNotFoundException" will be thrown

Guess you like

Origin blog.csdn.net/MARVEL_3000/article/details/114441089