學習打卡--反射(1)
阿新 • • 發佈:2019-04-25
-- obj class a 理解 one ble system bject method
今天看了一整天的博客,終於看懂了反射,但一些很細節的地方還是有點難理解,可能自己腦袋真的很笨吧。
反射說白了就是通過Class類、Constructor(構造器)類、Field類、Method類來分別獲取某個類的類、接口和父類、構造方法、成員屬性以及成員方法的一些信息等等。
註意:Class類和普通的class不同,Class類是實際存在的一個類,它的實例對象就是一個類。
以下附上今日的代碼:
package com.sy.reflection; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class A extends Object implements ActionListener{ private int a = 3; public Integer b = new Integer(4); public A() {} public A(int id,String name) {} public int abc(int id,String name) {return 0;} public void actionPerformed(ActionEvent e) {} }
package com.sy.reflection; import java.lang.reflect.*; public class B { public static void main(String[] args) { A r = new A(); Class temp = r.getClass(); try { System.out.println("反射類中所有公有的屬性"); Field[] fb = temp.getFields(); for(int j = 0;j<fb.length;j++) { Class cl = fb[j].getType(); System.out.println("fb:"+cl); } System.out.println("反射類中所有的屬性"); Field[] fa = temp.getDeclaredFields(); for(int j = 0;j<fa.length;j++) { Class cl = fa[j].getType(); System.out.println("fa:"+cl); } System.out.println("反射類中所有私有的屬性"); Field fc = temp.getDeclaredField("a"); fc.setAccessible(true); Integer i = (Integer)fc.get(r); System.out.println(i); }catch(Exception e){ e.printStackTrace(); } } }
package com.sy.reflection; import java.io.*; import java.lang.reflect.*; public class sampleInterface { public static void main(String[] args)throws Exception { A raf = new A(); printInterfaceName(raf); } public static void printInterfaceName(Object o) { Class c = o.getClass(); Class[] theInterfaces = c.getInterfaces(); for(int i = 0;i<theInterfaces.length;i++) System.out.println(theInterfaces[i].getName()); Class theSuperclass = c.getSuperclass(); System.out.println(theSuperclass.getName()); } }
package com.sy.reflection;
import java.lang.reflect.*;
public class sampleConstructor {
public static void main(String[] args) {
A r = new A();
printConstructors(r);
}
public static void printConstructors(A r) {
Class c = r.getClass();
String className = c.getName();
try {
//用getConstructors()方法獲取了反射類的構造方法的集合
Constructor[] theConstructors = c.getConstructors();
for(int i = 0;i<theConstructors.length;i++) {
//用Constructor類的getParameterTypes()獲取該構造方法的參數類型數組
Class[] parameterTypes = theConstructors[i].getParameterTypes();
System.out.print(className+"(");
for(int j = 0;j<parameterTypes.length;j++)
System.out.println(parameterTypes[j].getName()+" ");
System.out.println(")");
}
}catch(Exception e) {
e.printStackTrace();
}
}
}
package com.sy.reflection;
import java.lang.reflect.*;
public class sampleMethod {
public static void main(String[] args) {
A p = new A();
printMethods(p);
}
public static void printMethods(Object o) { //向上轉型
Class c = o.getClass();
String className = c.getName();
Method[] m = c.getMethods();//返回方法數組
for(int i=0; i<m.length; i++) {
System.out.print(m[i].getReturnType().getName());//打印出返回值類型名稱
System.out.print(" "+m[i].getName()+"(");
Class[] parameterTypes = m[i].getParameterTypes();//返回參數類型的一個類的數組
for(int j=0; j<parameterTypes.length; j++){
System.out.print(parameterTypes[j].getName()); //打印出參數類型的名稱
//如果有多個參數,就打印出","表示間隔
if(parameterTypes.length>j+1){
System.out.print(",");
}
}
System.out.println(")");
}
}
}
學習打卡--反射(1)