java Class<>
时间: 2023-12-10 18:06:33 浏览: 59
Java中Class对象详解共2页.pdf.zip
The `Class<>` is a class in Java that represents the metadata of a Java class at runtime. It provides information about the class, such as its name, its superclass, the interfaces it implements, its constructors, fields, and methods.
The `Class<>` class is used for reflection, which is the ability of a Java program to inspect and modify its own structure and behavior at runtime. With reflection, a Java program can dynamically load a class, examine its methods and fields, and invoke its methods and constructors.
Here is an example of using the `Class<>` class to get information about a Java class:
```java
Class<?> clazz = String.class;
System.out.println("Class name: " + clazz.getName());
System.out.println("Superclass: " + clazz.getSuperclass().getName());
System.out.println("Interfaces: " + Arrays.toString(clazz.getInterfaces()));
```
This code gets the `Class<>` object for the `String` class, and then prints out its name, superclass, and interfaces. The output would be:
```
Class name: java.lang.String
Superclass: java.lang.Object
Interfaces: [java.io.Serializable, java.lang.Comparable, java.lang.CharSequence]
```
阅读全文