Reflection of Java Basics (very important) Reflection is the soul of frame design
(Use Prerequisites: The Class,class class that must first get the byte code represented is used to represent the. class file (bytecode))
I. Overview of Reflection
The Java reflection mechanism is in the running state, for any class, all the properties and methods of the class can be known, and any one of its methods and properties can be invoked for any object; This dynamically acquired information, and the function of the method that dynamically invokes the object, is called the reflection mechanism of the Java language.
To dissect a class, you must first obtain the byte-code file object for that class. And anatomy uses the methods in class classes. So get to the object of the class type for each byte-code file first.
The above summary is what is reflection
Reflection is the mapping of various components in Java classes into Java objects
For example: A class has: member variables, methods, construction methods, packets and so on information, the use of reflection technology can be a class of anatomy, each part of the mapping into a single object.
(actually: In a class, these member methods, construction methods, and a class in the join class are described)
As the graph is the normal loading process of a class: the principle of reflection is in relation to class objects.
Familiar with the loading time: The class object is to read the class file into memory, and create a class object for it.
This class object is very special. Let's take a look at this class first.
Second, view class class in Java API detailed (1.7 API)
How to read Java API see--string string processing in Java basics
Instances of class classes represent classes and interfaces in a running Java application. That is, there are more than n instances in the JVM and each class has the class object. (including basic data types)
Class does not have a public constructor method. Class objects are automatically constructed by the Java virtual machine and by invoking the DefineClass method in the ClassLoader when loading the class. That means we don't have to deal with the creation ourselves, the JVM has helped us create it.
There are no public constructs, there are 64 or so many methods. Which one is the following?
Third, the use of reflection (here using the Student class to do demo)
Write a student class first.
1, get the class object three ways 1.1 object--> getclass ();
1.2 Any data type (including basic data types) has a "Static" Class attribute
1.3 Static method Via Class class: Forname (String className) (commonly used)
1.1 of these are due to the GetClass method in the object class, because all classes inherit the object class. Thus invoking the object class to get the
<span style= "FONT-SIZE:18PX;" >package Fanshe;
/**
* Three ways to get the class object
* 1 object--> getclass ();
* 2 any data type (including basic data types) has a "Static" Class attribute
* 3 static method via Class class: Forname (String className) (commonly used)
*
*/
public class Fanshe {
public static void Main (string[] args) {
The first way to get the class object
Student stu1 = new Student ()//This new creates a Student object, a Class object.
Class Stuclass = Stu1.getclass ();//Get Class object
System.out.println (Stuclass.getname ());
The second way to get the class object
Class stuClass2 = Student.class;
System.out.println (Stuclass = = stuClass2)//judge the first way to get the class object and the second way to get whether the same
The third way to get the class object
try {
Class STUCLASS3 = Class.forName ("Fanshe.") Student ");/Note that this string must be a true path, a classpath with package names, and a package name. Class Name
System.out.println (STUCLASS3 = = StuClass2)//Determine whether three ways to get the same class object
catch (ClassNotFoundException e) {
E.printstacktrace ();
}
}
}</span>
Note: During run time, a class is generated with only one class object.
Three ways to use the third, the first object has a reflection of what to do. The second need to import the package of classes, relying too strong, do not guide the package to throw compilation errors. Generally the third, a string can be passed in or written in the configuration file in a variety of ways.
2. Get the construction method through reflection and use:
Student class:
Package fanshe;
public class Student {
---------------Construction Method-------------------
(default method of construction)
Student (String str) {
System.out.println ("(default) construction method s =" + str);
}
Non-parametric construction method
Public Student () {
SYSTEM.OUT.PRINTLN ("The public, parameterless construction method has been invoked ...) ");
}
A method of constructing a parameter
Public Student (char name) {
System.out.println ("Name:" + name);
}
A method of constructing with multiple parameters
Public Student (String name, int age) {
System.out.println ("Name:" +name+ "Age:" + ages);//There is a problem with the efficiency of the execution, which will be resolved later.
}
Protected construction methods
Protected Student (Boolean N) {
SYSTEM.OUT.PRINTLN ("protected construction method n =" + N);
}
Private construction methods
Private Student (int age) {
SYSTEM.OUT.PRINTLN ("Private construction Method Age:" + ages);
}
}
A total of 6 construction methods;
Test class:
Package fanshe;
Import Java.lang.reflect.Constructor;
/*
* The class object can be used to get the constructor method, member variable, member method, and to access the member;
*
* 1. Get the construction method:
* 1). Batch method:
* Public constructor[] GetConstructors (): All "publicly owned" construction methods
&