Java中的反射總結

來源:互聯網
上載者:User

標籤:logs   getname   函數   ati   member   interface   extend   image   class類   

反射是擷取運行時類資訊,即常量區中的Class資訊。

要擷取類資訊,必然需要依據,不然系統怎麼指定你要擷取那個類資訊,

類資訊在java中就是Class類的一個對象,它是一個java類抽象,換句話說它代表了一個‘類‘(類本身是一種事物的抽象,在程式中它代表了這一事物,Class就是對這種類的一種抽象),

三種擷取方式,

1).Class名稱的字串  Class.forName

2).根據Class擷取, Integer.class

3).根據對象擷取  (new Integer()).getClass

值得一提的是所有的Class對象的class仍然是class

下面來說一下這個Class類,

首先Class實現了Type,Type並沒任何函數,是一個空介面,它只是代表這是一個類型

package java.lang.reflect;public interface Type {}
View Code

另外它實現了AnnotatedElement介面,表示這是一個可以使用註解的元素。

package java.lang.reflect;import java.lang.annotation.Annotation;public interface AnnotatedElement {     boolean isAnnotationPresent(Class<? extends Annotation> annotationClass);    <T extends Annotation> T getAnnotation(Class<T> annotationClass);    Annotation[] getAnnotations();    Annotation[] getDeclaredAnnotations();}
View Code

GenericDeclaration介面,表示這是一個可以被泛型化的元素,該介面擷取一個TypeVariable數組,表示泛型參數,TypeVariable實現了Type介面,也就是說泛型的參數也是一個類型

package java.lang.reflect;public interface GenericDeclaration {    public TypeVariable<?>[] getTypeParameters();}
View Code

整個類別關係如下:

Class下彙總了Field(欄位),Method(方法),Constructor(構造方法)

這三個成員均繼承自Member,也就是反射成員。

package java.lang.reflect;public interface Member {    public static final int PUBLIC = 0;    public static final int DECLARED = 1;    public Class<?> getDeclaringClass();    public String getName();    public int getModifiers();    public boolean isSynthetic();}
View Code

 

Java中的反射總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.