反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。【翻译于 官方文档】
This article will introduce the knowledge of reflection from the following aspects: reflection of
class using
method
Reflection of constructor
Reflection of member variables
In the object-oriented world, everything is an object. Classes are objects, and classes are instance objects of the java.lang.Class class. In addition, the class class can only be new by the Java virtual machine. Any class is an instance object of the Class class. This instance object has three expression methods:
public class User{
}
public class ClassTest{
User u=new User();
//Method 1 :
Class c1=User.class;
//Method 2:
Class c2=u.getClass();
//Method 3:
Class c3=Class.forName(" com.forezp.User");
//You can create an instance object of the class through the type of the class
User user=(User)c1.newInstance();
}
Class.forName (full name of the class); This method not only represents the type of the class, but also represents the dynamically loaded class. Classes loaded at compile time are statically loaded, and classes loaded at runtime are dynamically loaded.
Basic data types and void keywords are instances of the Class class; the name of the class can be obtained through getame();getSimpleName() .
Class c1=String.class;
Class c2=int.class;
Class c3=void.class;
System.out.println(c1.getName());
System.out.println(c2.getSimpleName());
Get all methods of the class and print them out:
public static void printClassInfo(Object object){
Class c=object .getClass();
System.out.println("Name of class: "+c.getName());
/**
* A member method is a method object
* getMethod() gets all the public methods, including those inherited from the parent class
* getDeclaredMethods() gets all the methods of the class, including private, but not inherited Methods.
*/
Method[] methods= c.getMethods();//Get methods
//Get all methods, including private,c.getDeclaredMethods();
for(int i=0;i
System.out.print(methods[i].getName()+"(");
Class[] parameterTypes=methods[i].getParameterTypes();
for(Class class1:parameterTypes){
System.out.print(class1.getName()+",");
}
System.out.println(")");
}
}
public class ReflectTest {
public static void main(String[] args){
String s="ss";
ClassUtil.printClassInfo(s);
}
}
Run:
类的名称:java.lang.String booleanequals(java.lang.Object,) java.lang.StringtoString() inthashCode() …
Class c=o.getClass();
/**
* getFileds() gets public
* getDeclaredFields() gets all
*/
Field[] fileds=c.getDeclaredFields();
for (FIELD F: FILEDS) {
// The type of member variables
Class Filedtype = f.gettype ();
System.Println (Filedtype.getName ()+"" "" +f.getName());
}
## }
public static void main(String[] args){
String s="ss";
’ s ’ s =" s s t t ’ ‐ ‐ ‐ ‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ printClassInfo(s);
ClassUtil.printFiledInfo(s);
}
Run:
[C value int hash long serialVersionUID [Ljava.io.ObjectStreamField; serialPersistentFields java.util.Comparator CASE_INSENSITIVE_ORDER int HASHING_SEED int hash32
public static void printConstructInfo(Object o){
Class c=o.getClass();
Constructor[] constructors=c.getDeclaredConstructors();
for (Constructor con:constructors){
System.out.print(con.getName()+”(“);
Class[] typeParas=con.getParameterTypes();
for (Class class1:typeParas){
System.out.print(class1.getName()+” ,”);
}
System.out.println(“)”);
}
}
public static void main(String[] args){
String s="ss";
//ClassUtil.printClassInfo(s);
//ClassUtil.printFiledInfo(s);
ClassUtil.printConstructInfo(s);
}
运行:
java.lang.String([B ,) java.lang.String([B ,int ,int ,) java.lang.String([B ,java.nio.charset.Charset ,) java.lang.String([B ,java.lang.String ,) java.lang.String([B ,int ,int ,java.nio.charset.Charset ,) java.lang.String(int ,int ,[C ,) java.lang.String([C ,boolean ,) java.lang.String(java.lang.StringBuilder ,) java.lang.String(java.lang.StringBuffer ,) ...
获取一个方法:需要获取方法的名称和方法的参数才能决定一个方法。
方法的反射操作:
method.invoke(对象,参数列表);
举个例子:
class A{
public void add(int a,int b){
System.out.print(a+b);
}
public void toUpper(String a){
System.out.print(a.toUpperCase());
}
}
public static void main(String[] args) {
A a=new A();
Class c=a.getClass();
try {
Method method=c.getMethod("add",new Class[]{int.class,int.class});
//也可以 Method method=c.getMethod("add",int.class,int.class);
//方法的反射操作
method.invoke(a,10,10);
}catch (Exception e){
e.printStackTrace();
}
}
运行:
20
本篇文章已经讲解了java反射的基本用法, 它可以在运行时判断任意一个对象所属的类;在运行时构造任意一个类的对象;在运行时判断任意一个类所具有的成员变量和方法;在运行时调用任意一个对象的方法;生成动态代理。
The above is the detailed content of Summary of knowledge about Java reflection mechanism that needs to be mastered. For more information, please follow other related articles on the PHP Chinese website!