1. 程式人生 > >Java中Class類詳解、用法以及泛化

Java中Class類詳解、用法以及泛化

在前面我們將類的載入的時候,類載入一共有5步,載入,驗證,準備,解析和初始化。其中載入階段,除了將位元組碼載入到方法區,還生成了這個了的Java.lang.Class物件。那麼這個Class物件到底有什麼用呢?

前面的關於反射的文章,我們多次都用到了Class類,可以用這個類建立對應類的例項物件,這可以看做是Class類的一個作用。但是這個Class類和對應類本身又有什麼聯絡和區別呢?比如new A()和A.class.newInstance(),聯絡和區別?

1.什麼是Class類&Class類的作用

Class類說變了也是一個類,只不過是一個比較特殊的類。特殊在哪裡呢?特殊在這是一個在類載入過程中由虛擬機器生成的,由於表示被載入類的型別資訊的物件。簡單地說,我們建立一個int變數,那麼這個int變數是個整數型別,那麼我們怎麼知道這個型別是整數型別呢?就是通過這個Class類來直到的。 java是面向物件程式設計的,java中幾乎所有的資料都是物件,那麼是物件,就必須知道自己到底是哪一種型別的物件。於是Class類便順勢而生了。 Class類的作用放在這邊講有點不太合適,要想了解Class類的作用建議讀完下面的內容。我自己理解的話,Class類的作用,本質上講,就是前面所說的,它代表著一個類的型別資訊。正是因為這個特殊作用的存在,Class類能夠實現它所代表的這個類的所有功能,包括建立這個類的例項,獲得所有的建構函式,方法,欄位值等等,可以說無所不能。

2.Class類裡面儲存了什麼

既然我們知道Class類是表示型別的一個特殊的類,那麼它是怎麼表示某個類或者物件的型別呢?或者說,它裡面儲存了什麼呢?通過反射機制我們知道,Class裡面儲存了對應類的幾乎所有的資訊,當然這些資訊是未初始化的資訊,比如所有的方法,所有的建構函式,所有的欄位(成員屬性)等等。 那這些資訊是怎麼儲存進去的呢?好吧,這個應該去深入的參考java虛擬機器規範吧,畢竟Class這個類的建構函式是私有的,可能是底層的C++程式碼實現的。

3.class類的靜態方法forName()

Modifier and Type Method and Description
<U> Class<? extends U>
(Class<U> clazz) Casts this Class object to represent a subclass of the class represented by the specified class object.
T cast(Object obj) Casts an object to the class or interface represented by this Class object.
boolean Returns the assertion status that would be assigned to this class if it were to be initialized at the time this method is invoked.
static Class<?> Returns the Class object associated with the class or interface with the given string name.
static Class<?> Returns the Class object associated with the class or interface with the given string name, using the given class loader.

上述兩個方法摘自javadoc,詳細的內容可以去javadoc看。最常用的是第一個,注意這個className一定是全限定名,而不能是簡單名稱。也就是包名.類名

4.class類的所有方法(包括靜態方法)

Modifier and Type Method and Description
<U> Class<? extends U> (Class<U> clazz) Casts this Class object to represent a subclass of the class represented by the specified class object.
T cast(Object obj) Casts an object to the class or interface represented by this Class object.
boolean Returns the assertion status that would be assigned to this class if it were to be initialized at the time this method is invoked.
static Class<?> Returns the Class object associated with the class or interface with the given string name.
static Class<?> Returns the Class object associated with the class or interface with the given string name, using the given class loader.
Returns an array of AnnotatedType objects that represent the use of types to specify superinterfaces of the entity represented by this Class object.
Returns an AnnotatedType object that represents the use of a type to specify the superclass of the entity represented by this Class object.
(Class<A> annotationClass) Returns this element's annotation for the specified type if such an annotation is present, else null.
Returns annotations that are present on this element.
Returns annotations that are associated with this element.
Returns the canonical name of the underlying class as defined by the Java Language Specification.
Class<?>[] Returns an array containing Class objects representing all the public classes and interfaces that are members of the class represented by this Class object.
Class<?> Returns the Class representing the component type of an array.
(Class<?>... parameterTypes) Returns a Constructor object that reflects the specified public constructor of the class represented by this Class object.
Returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object.
Returns this element's annotation for the specified type if such an annotation is directly present, else null.
Returns annotations that are directly present on this element.
Returns this element's annotation(s) for the specified type if such annotations are either directly present or indirectly present.
Class<?>[] Returns an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object.
Returns a Constructor object that reflects the specified constructor of the class or interface represented by this Class object.
Returns an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object.
Field Returns a Field object that reflects the specified declared field of the class or interface represented by this Class object.
Field[] Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object.
Returns a Method object that reflects the specified declared method of the class or interface represented by this Class object.
Returns an array containing Method objects reflecting all the declared methods of the class or interface represented by this Class object, including public, protected, default (package) access, and private methods, but excluding inherited methods.
Class<?> If the class or interface represented by this Class object is a member of another class, returns the Class object representing the class in which it was declared.
Class<?> Returns the immediately enclosing class of the underlying class.
If this Class object represents a local or anonymous class within a constructor, returns a Constructor object representing the immediately enclosing constructor of the underlying class.
If this Class object represents a local or anonymous class within a method, returns a Method object representing the immediately enclosing method of the underlying class.
T[] Returns the elements of this enum class or null if this Class object does not represent an enum type.
Field Returns a Field object that reflects the specified public member field of the class or interface represented by this Class object.
Field[] Returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object.
Type[] Returns the Types representing the interfaces directly implemented by the class or interface represented by this object.
Type Returns the Type representing the direct superclass of the entity (class, interface, primitive type or void) represented by this Class.
Class<?>[] Determines the interfaces implemented by the class or interface represented by this object.
(String name, Class<?>... parameterTypes) Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object.
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.
int Returns the Java language modifiers for this class or interface, encoded in an integer.
Returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String.
Gets the package for this class.
URL Finds a resource with a given name.
Gets the signers of this class.
Returns the simple name of the underlying class as given in the source code.
Class<? super T> Returns the Class representing the superclass of the entity (class, interface, primitive type or void) represented by this Class.
Return an informative string for the name of this type.
Returns an array of TypeVariable objects that represent the type variables declared by the generic declaration represented by this GenericDeclaration object, in declaration order.
boolean Returns true if this Class object represents an annotation type.
boolean Returns true if an annotation for the specified type is present on this element, else false.
boolean Returns true if and only if the underlying class is an anonymous class.
boolean Determines if this Class object represents an array class.
boolean Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter.
boolean Returns true if and only if this class was declared as an enum in the source code.
boolean Determines if the specified Object is assignment-compatible with the object represented by this Class.
boolean Determines if the specified Class object represents an interface type.
boolean Returns true if and only if the underlying class is a local class.
boolean Returns true if and only if the underlying class is a member class.
boolean Determines if the specified Class object represents a primitive type.
boolean Returns true if this class is a synthetic class; returns false otherwise.
T Creates a new instance of the class represented by this Class object.
Returns a string describing this Class, including information about modifiers and type parameters.
Converts the object to a string.

順便說一下這裡的newInstace()方法,因為這個方法比較常用,但是這個會呼叫對應類的預設無引數建構函式,如果要使用呼叫有引數的構造器建立一個物件,那麼,就必須獲得這個構造器,然後再使用構造器呼叫了。具體的方式向下面這樣

//假設A的含參構造器含有4個引數,分別是int,int,string,和類B

Constructor cons = A.class.getConstructor(int.class, int.class, String.class, B.class);

B b = new B();

//獲得A的例項。

A a  = cons.newInstance(5,5,"Jack",b);