(1) The Java.lang.Class class represents classes and interfaces in a running Java application. The class object can be obtained by getclass () or static method.
such as: String str= "HelloWorld".
Java.lang. Class Strclass=str.getclass ()
or (class name. Class)//Gets the class object.
or loaded by loader
Class className = Class.forName ("java.lang.String");
After you get the class object (category) You can have the following methods
GetName () Returns the class object name as a string
(2), on-demand loading Method forname () or ClassLoader class LoadClass () method
Java.lang.Class bookclass=java.lang.class.forname ("book"); Book is a class
or Java.lang.Class Bookclass=classloader.loadclass ("book").
Add: Class information is added to the. class file at compile time, and the JVM checks to see if the corresponding class object has been loaded and, if not, looks for the corresponding. class file loading.
(3), reflection mechanism
Definition: Java reflection mechanism is in the running state, for any class, can know all the properties and methods of the class, for any object, can call any one of its methods;
This dynamically acquired information, and the ability to dynamically invoke the method of the object, is called the reflection mechanism of the Java language. Note: Dynamic two words.
Get category by object: Java.lang.Class Aclass=obj.getclass ();
Object attributes are obtained by category, (Static and dynamic) Field Field=aclass.getfield (fieldName);
Object Obj=field.get (obj) that gets the property from the property;
Get a method from a category
public object InvokeMethod (object owner, String methodname, object[] args) throws Exception {
object[] args: Parameter array required by method
Class Ownerclass = Aclass.getclass ();
class[] Argsclass = new Class[args.length];
for (int i = 0, j = args.length I < J; i++) {
Argsclass[i] = Args[i].getclass ();
}
Get this method
Method method = Ownerclass.getmethod (methodname, Argsclass);
Executing this method returns a value of object and is also the return value of the method.
Return Method.invoke (owner, args);
(4) Reflection application:
Methods to invoke objects dynamically
Entity classes
public class Reflectiondemo {
public void method1 (String str) {
System.out.println ("method1 () ==>" + "string==>" + str);
}
public void Method2 (String str, int n) {
System.out.println ("Method2 () ==>" + "string==>" + str + "int==>"
+ N);
}
public void method3 (String str, int n, char ch) {
System.out.println ("method3 () ==>" + "string==>" + str + "int==>"
+ N + "char==>" + ch);
}
public string method4 (String str, double n) {
return str + N;
}
}
public class Reflectiondemo {
public void method1 (String str) {
System.out.println ("method1 () ==>" + "string==>" +str);
}
public void Method2 (String str,int N) {
System.out.println ("Method2 () ==>" + "string==>" +str+ "int==>" +n);
}
public void Method3 (String str,int N,char ch) {
System.out.println ("method3 () ==>" + "string==>" +str+ "int==>" +n+ "char==>" +ch);
}
public string method4 (String str,double N) {
return str+n;
}
}
public class Invokedemo {
Defining Method Objects
Private Method method = null;
To define a method return value
Private Object result = NULL;
Private Map HashMap = new HashMap ();
Encapsulates a method name and an array of parameter types for the HashMap
public void Initmap (Object obj) {
Class className = Obj.getclass ();
method []methos = Classname.getmethods ();
for (int i = 0; i<methos.length; i++) {
String name = Methos[i].getname ();
Class []args = Methos[i].getparametertypes ();
Hashmap.put (name, args);
}
}
/**
*
* @param obj executes the specific object of the method
* Specific objects to be executed @param methodname
* @param args to execute the specific parameters of the method
* @return
* @throws InvocationTargetException
* @throws illegalaccessexception
* @throws IllegalArgumentException
* @throws nosuchmethodexception
* @throws SecurityException
*/
public Object Getresultbymethod (object obj,string methodname,object[] args) {
Class className = Obj.getclass ();
try {
method = Classname.getmethod (methodname, (class[)) Hashmap.get (methodname));
result = Method.invoke (Obj,args);
catch (SecurityException e) {
E.printstacktrace ();
catch (Nosuchmethodexception e) {
E.printstacktrace ();
}catch (IllegalArgumentException e) {
E.printstacktrace ();
catch (Illegalaccessexception e) {
E.printstacktrace ();
catch (InvocationTargetException e) {
E.printstacktrace ();
}
return result;
}
Test
public static void Main (String []args) {
Invokedemo ind = new Invokedemo ();
Reflectiondemo ref = new Reflectiondemo ();
Ind.initmap (ref);
list<string> list = new arraylist<string> ();
for (int i = 1; i<=4;i++) {
List.add ("method" +i);
}
First method Shape parameter
Object []REALARGS1 = new object[1];
Realargs1[0] = "HelloWorld";
The second method-shape parameter
Object []REALARGS2 = new object[2];
Realargs2[0] = "HelloWorld";
REALARGS2[1] = 12;
A third method-shape parameter
Object []REALARGS3 = new object[3];
Realargs3[0] = "HelloWorld";
REALARGS3[1] = 14;
REALARGS3[2] = ' C ';
The fourth method-shape parameter
Object []REALARGS4 = new object[2];
Realargs4[0] = "HelloWorld";
REALARGS4[1] = 23;
Object result = null;
Calling Method Method1
result = Ind.getresultbymethod (ref, List.get (0), REALARGS1);
Calling Method Method2
result = Ind.getresultbymethod (ref, List.get (1), REALARGS2);
Calling Method Method3
result = Ind.getresultbymethod (ref, List.get (2), REALARGS3);
Calling Method Method4
result = Ind.getresultbymethod (ref, List.get (3), REALARGS4);
if (result!= null) {
SYSTEM.OUT.PRINTLN ("Result:" +result);
}
}
}