Java: Lớp Class
Trong một chương trình Java đang thực thi, các thể hiện của lớp Class đại diện cho các lớp và giao diện (interface).
enum là một loại lớp và annotation là một loại giao diện.
Ngay cả một mảng thuộc về một lớp được thể hiện như một đối tượng Class được chia sẻ bởi tất cả các mảng có cùng kiểu phần tử và số thứ nguyên.
Các kiểu dữ liệu Java nguyên thủy như boolean, byte, char, short, int, long, float và double cũng như từ khóa void cũng được biểu diễn dưới dạng các đối tượng Class.
Lớp Class không có hàm tạo công khai. Thay vào đó, các đối tượng của Class được JVM xây dựng tự động khi các lớp được tải và bằng các lệnh gọi đến phương thức defineClass() trong trình nạp lớp.
Bảng sau đây liệt kê một số phương thức thường được sử dụng của lớp Class.
Phương thức |
Mô tả |
---|---|
static Class forName(String className) |
The method returns the Class object associated with the class or interface with the given string name. |
static Class forName(String name, boolean initialize, ClassLoader loader) |
The method returns the Class object associated with the class or interface with the given string name, using the given class loader. |
Class[]getClasses() |
The method returns an array containing Class objects representing all the public classes and interfaces that are members of the class represented by this Class object. |
Field getField(String name) |
The method returns a Field object that reflects the specified public member field of the class or interface represented by this Class object. |
Class[]getInterfaces() |
The method determines the interfaces implemented by the class or interface represented by this object. |
Method getMethod(String name, Class[] parameterTypes) | The method returns a Method object that reflects the specified public member method of the class or interface represented by this Class object. |
int getModifiers() | The method returns the Java language modifiers for this class or interface, encoded in an integer. |
String getName() | The method returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String. |
URL getResource(String name) | The method finds a resource with a given name. |
Class getSuperclass() | The method returns the Class representing the superclass of the entity (class, interface, primitive type, or void) represented by this Class. |
boolean isArray() |
The method determines if this Class object represents an array class. |
boolean isInstance(Object obj) | Determines if the specified Object is assignment-compatible with the object represented by this Class. |
boolean isInterface() | The method determines if the specified Class object represents an interface type. |
String toString() | The method converts the object to a string. |
Code Snippet 5 shows the use of some of the methods of Class class. Code Snippet 5:
class ClassClass extends MathClass{ public ClassClass(){}
}
public class TestClass {
public static void main(String[] args) { ClassClass obj = new ClassClass();
System.out.println(“Class is: “ + obj.getClass());
}
}
The code shows the use of the getClass() method of the Class class.