Today we learned about reflection, and we can find the construction method and all the member methods in the class by reflection. People who do not understand the structure of this class can easily use the reflection mechanism.
Master launches are primarily known for these classes, which are located in the Java.long.reflect package:
Class: Represents a Class
Constructor class: A construction method that represents fatigue
Field class: Represents a member variable in a class
Method Class: Methods for representing classes
Import java.lang.reflect.*;
/**
This is an example of a reflection mechanism, including how to use reflection to find an argument-free, constructive method, and other methods.
@author Lzl
@version v1.1
*/
public class reflectdemos{
/**
Non-parametric construction method
*/
Public Reflectdemos () {}
/**
Construction method with one parameter
@param age
*/
Public Reflectdemos (int.) {
System.out.println ("My Age is 15");
}
/**
Construction method with two parameters
@param age
@param name
*/
Public Reflectdemos (String Name,int age) {
System.out.println ("My name is A and age is 18");
}
/**
No parameter member method
*/
public void Fun1 () {
System.out.println ("This is fun1 Constructor");
}
/**
A method with two parameters
@param age
@param name
*/
public void Fun2 (String name, int age) {
System.out.println ("This is fun2 Constructor" +name+age);
}
/**
Construction method with one parameter
@param age
@param name
*/
public void Fun3 (String name) {
System.out.println ("This is Fun3 Constructor" +name);
}
/**
Using reflection mechanism to get all the examples of construction methods
@param className class Name
*/
public static void GetConstructors (String className) {
try{
Class ClassInfo = Class.forName (className); Pass in a class name and get the corresponding object
Constructor con[] = classinfo.getconstructors (); All construction methods in this class are obtained by GetConstructors ().
Constructors are stored in memory like stacks, so they are in the order of advanced post-out. Con[0] Represents the public A (String Name,int Age) constructor
/**
Newinstance ()
Creates a new instance of the class represented by this class object. Equivalent to New Reflectdemos ();
*/
Con[0].newinstance (New object[]{"Lzl", 18});
Con[1].newinstance (New object[]{18});
Con[2].newinstance ();
}catch (Exception e) {}
}
/**
Get an instance of all the methods in a class
@param className class Name
*/
public static void GetMethods (String className) {
try{
Class ClassInfo = Class.forName ("Reflectdemos"); Pass in a class name and get the corresponding object
Object obj = Classinfo.getconstructor (). newinstance (); Call the parameterless constructor to generate a new instance object
/**
Method GetMethod (String name, Class<?> .... parametertypes)
Returns a method object that reflects the specified public member methods for the class or interface represented by this class object.
Object Invoke (Object obj, object ... args)
Invokes the underlying method represented by this method object for the specified object with the specified argument
*/
Call the no-argument member method Fun1 ()
Method MT1 = Classinfo.getmethod ("fun1");
Mt1.invoke (obj);
Call a member method of 1 arguments fun3 ()
Method mt2 = Classinfo.getmethod ("Fun3", String.class);
Mt2.invoke (obj,new object[]{"Lzl"});
Call a member method of 2 arguments fun3 ()
Method mt3 = Classinfo.getmethod ("fun2", String.class,int.class);
Mt3.invoke (obj,new object[]{"Lzl", 22});
}catch (Exception e) {}
}
/**
Get an instance of the specified method
@param className class Name
@param FuncName Method Name
@param Paras Method parameters
@return Returns a Boolean type
*/
public static Boolean process (String classname,string Funcname,object[] paras) throws exception{
Class ClassInfo = Class.forName (className);
Forming a function parameter sequence------> popular saying is to find the parameter member passed in the method
Class c[] = new Class[paras.length];
for (int i=0;i<c.length;i++) {
C[i] = Paras[i].getclass ();
}
Get the parameterless constructor, instantiate the object
Object obj = Classinfo.getconstructor (). newinstance ();
Ways to get classes
/**
Method GetMethod (String name, Class<?> .... parametertypes)
Returns a method object that reflects the specified public member methods for the class or interface represented by this class object.
Object Invoke (Object obj, object ... args)
Invokes the underlying method represented by this method object for the specified object with the specified argument
*/
Method MT = Classinfo.getmethod (funcname,c);
Mt.invoke (Obj,paras);
return true;
}
public static void Main (String []args) {
try{
Boolean flag = reflectdemos.process ("Reflectdemos", "fun1", New object[]{});
}catch (Exception e) {}
}
}
Today I also learned how to make a Help document, first of all help the document to write comments, in accordance with the format of the following comments
/**
@author Author name
@version version number
*/
/**
@param parameter Interpretation
@return Explanation
*/
In the Doc window, enter: javadoc-d file name-author-version class name then a help document is generated.
For example:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Reflection mechanism-invoking constructors and member methods, authoring help documents