什麼是反射:java
反射:框架
在程序運行中,對於任意一個類,都可以知道這個類的全部屬性和方法;dom
對於任意一個對象,都可以調用它的任意方法和屬性;ide
這種動態獲取信息以及動態調用對象方法的功能稱爲java語言的反射機制。工具
簡而言之,反射就2個做用,學習
一、能夠在程序運行中獲取任意類的屬性、方法、構造器、註解等信息;this
二、能夠在程序運行時調用類中的任意的屬性和方法,包括私有。【這塊有疑問,應該是對象而不是任意類】spa
在開發中咱們通常在代理模式、工具類(好比DbUtils、JSON)、框架中使用。代理
反射涉及到的類說明:code
一、Class:
這是反射的核心類,也是一個泛型類,java規定任意類型都有Class對象。經常使用方法,參考jdk1.8API
1.getAnnotation(Class annotationClass)
Returns this element`s annotation for the specified type if such an annotation is present,else null.
獲取指定類型的註解對象,找到就返回不然就返回null.
2.getAnnotations()
Returns annotations that are present on this element.
返回對應類上的全部註解對象。
3.getClassLoader()
Returns the class loader for the class.
返回類的加載器對象。
4.getDeclaredFields()
Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object.
獲取對應類中全部的屬性,不管什麼訪問修飾符,但不包括繼承的屬性
5.getFields()
Returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object.
獲取對應類中全部的公共屬性,也包括繼承來的公共屬性。
6.getDeclaredMethods()
Returns an array containing Method objects reflecting all the declared methods of class or interface represented by this Class object,including public,protected,default(package)access,and private methods,but excluding inherited methods.
獲取對應類中的全部方法,不管什麼訪問修飾符,可是不包括繼承來的方法。
7.getMethods()
Returns an array containing Method objects reflecting all the public methods of the class or interface represented by this Class object ,including those declared by the class or interface and those inherited from superclasses and superinterfaces.
獲取對應類中的全部公共方法,也包括繼承來的公共方法。
8.newInstance()
Creates a new instance of the class represented by the Class object.
建立對應類的對象。
以上就是Class類的經常使用方法,沒列出的方法參考java的API。
反射的使用:
以Dog類爲題進行學習Class的使用:
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
public class Dog{ private int id; public String name; protected String type; private void eat(){ System.out.println(「通常狗都喜歡啃骨頭」); } private void eat(int count,String food){ System.out.println(「隔壁家的狗狗吃了」+count+」個」+food); } public void show(){ System.out.println(「編號:」+id+」,名稱:」+name+」,類型:」+type); } }
class對象建立的三種方式:
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
public static void main(String[] args) throws Exception{ Class<Dog> clsl; //第一種方式 clsl=Dog.class; //第二種方式 clsl = (class<Dog>) Class.forName(「cn.code404.domain.Dog」); //第三種方式 clsl = (Class<Dog>) new Dog().getClass(); System.out.println(clsl.getName()); }
獲取屬性和方法:
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
Class<Dog> class1 = Dog.class; //獲取指定類中的全部屬性,不包括繼承的屬性 Field[] arrDF = class1.getDeclaredFields(); for(Field f:arrDF){ //輸出字段名稱 System.out.println(f.getName()); } System.out.println(「****************」); //獲取指定類中的全部公共字段,包括繼承的屬性 Field[] arrF = class1.getFields(); for(Field f:arrF){ System.out.println(f.getName()); } System.out.println(「****************」); //獲取指定類中全部的方法,不包括繼承 Method[] arrDM = class1.getDeclaredMethods(); for(Method m:arrDM){ System.out.println(m.toString()); } System.out.println(「****************」); //獲取指定類中全部公共方法,包括繼承 Method[] arrM = class1.getMethods(); for(Method m:arrM){ System.out.println(m.toString()); }
調用私有方法:
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
public static void main(String[] args) throws Exception{ Class<Dog> cls = Dog.class; //執行私有方法不帶參數 //1、獲取要執行的方法對象 Method method1 = cls.getDeclaredMethod(「look」); //建立對應類的對象 Dog dog = cls.newInstance(); //設置忽略訪問校驗,true:不進行訪問校驗,false:進行訪問校驗 method1.setAccessible(true); //執行私有方法 method1.invoke(dog); //2、執行私有帶參數的方法 //一、獲取方法對象 Method method2 = cls.getDeclaredMethod(「eat」,int.class,String.class); //二、設置忽略訪問校驗 method2.setAccessible(true); //三、執行私有方法 method2.invoke(dog,2,」貓」); }
以上是反射的基本調用