Class
Instances of the class {@code Class} represent classes and
* interfaces in a running Java application. An enum is a kind of
* class and an annotation is a kind of interface. Every array also
* belongs to a class that is reflected as a {@code Class} object
* that is shared by all arrays with the same element type and number
* of dimensions. The primitive Java types ({@code boolean},
* {@code byte}, {@code char}, {@code short},
* {@code int}, {@code long}, {@code float}, and
* {@code double}), and the keyword {@code void} are also
* represented as {@code Class} objects.
上面那段文字的意思大概就是所有的类、枚举、接口、注解、数组、基本类型、还有void都是Class。
用一个简单的例子证明一下。
枚举
public enum EnumTest {
}
接口
public interface InterfaceTest {
}
注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD,ElementType.TYPE})
public @interface AnnotationTest {
}
测试例子
public class ClassTest {
public static void main(String[] args) {
if (Object.class instanceof Class) {
System.out.println(Object.class.getName()+ " :Object is Class");
}
if (String.class instanceof Class) {
System.out.println(String.class.getName() + " :String is Class");
}
if (Class.class instanceof Class) {
System.out.println(Class.class.getName() + " :Class is Class");
}
if (EnumTest.class instanceof Class) {
System.out.println(EnumTest.class.getName() + " :EnumTest is Class");
}
if (InterfaceTest.class instanceof Class) {
System.out.println(InterfaceTest.class.getName() + " :InterfaceTest is Class");
}
if (AnnotationTest.class instanceof Class) {
System.out.println(AnnotationTest.class.getName() + " :AnnotationTest is Class");
}
if (int[].class instanceof Class) {
System.out.println(int[].class.getName() + " : int[] is Class");
}
if (boolean.class instanceof Class) {
System.out.println(boolean.class.getName() + " : boolean is Class");
}
if (byte.class instanceof Class) {
System.out.println(byte.class.getName() + " : byte is Class");
}
if (char.class instanceof Class) {
System.out.println(char.class.getName() + " : char is Class");
}
if (short.class instanceof Class) {
System.out.println(short.class.getName() + " : short is Class");
}
if (int.class instanceof Class) {
System.out.println(int.class.getName() + " : int is Class");
}
if (long.class instanceof Class) {
System.out.println(long.class.getName() + " : long is Class");
}
if (float.class instanceof Class) {
System.out.println(float.class.getName() + " : float is Class");
}
if (double.class instanceof Class) {
System.out.println(double.class.getName() + " : double is Class");
}
if (void.class instanceof Class) {
System.out.println(void.class.getName() + " : void is Class");
}
// Integer.TYPE 和 int.class 是等价的
if (Integer.TYPE == int.class) {
System.out.println(true);
}
}
}
输出结果:
java.lang.Object :Object is Class
java.lang.String :String is Class
java.lang.Class :Class is Class
com.type.EnumTest :EnumTest is Class
com.type.InterfaceTest :InterfaceTest is Class
com.type.AnnotationTest :AnnotationTest is Class
[I : int[] is Class
boolean : boolean is Class
byte : byte is Class
char : char is Class
short : short is Class
int : int is Class
long : long is Class
float : float is Class
double : double is Class
void : void is Class
true
获取Class的三种方式
- 利用实例化对象调用getClass()方法获取Class
- 运用.class的方式来获取Class
例子 - 运行Class类的静态方法Class.forName(String className),className代表全限定类名。
public class Test {
public static void main(String[] args) {
System.out.println("第一种");
Test test = new Test();
System.out.println(test.getClass());
System.out.println("第二种");
System.out.println(Test.class);
System.out.println("第三种");
try {
System.out.println(Class.forName("com.type.Test"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
运行结果
第一种
class com.type.Test
第二种
class com.type.Test
第三种
class com.type.Test
Class类常用的方法
首先思考一下,在编写一个类的时候,是由哪些部分,比如有成员变量,成员方法,构造函数,继承哪个类,实现了哪些接口,加上注解,内部类等等。
1.有关注解的几个方法
getAnnotation()
getAnnotations()
getDeclaredAnnotation()
getDeclaredAnnotations()
getAnnotations() 与 getDeclaredAnnotations() 的区别
getAnnotations()除了该类的注解之外,还能获得父类的被声明允许继承的注解
getDeclaredAnnotations()只能获取该类的注解
看一下例子吧
注解类
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD,ElementType.TYPE})
public @interface AnnotationTest {
}
允许继承的注解类
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD,ElementType.TYPE})
public @interface AnnotationParentTest {
}
@AnnotationParentTest
public class ClazzParent {
}
@AnnotationTest
public class Clazz extends ClazzParent {
}
测试类
public class ClazzTest {
public static void main(String[] args) {
Class<?> clazz = null;
try {
clazz = Class.forName("com.type.Clazz");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
AnnotationTest annotation = clazz.getAnnotation(AnnotationTest.class);
System.out.println("getAnnotation() : " + annotation);
AnnotationParentTest annotation3 = clazz.getAnnotation(AnnotationParentTest.class);
System.out.println("getAnnotation() : " + annotation3);
Annotation[] annotations = clazz.getAnnotations();
for (Annotation annotation1 :annotations) {
System.out.println("getAnnotations() : " + annotation1);
}
Annotation declaredAnnotation = clazz.getDeclaredAnnotation(AnnotationTest.class);
System.out.println("getDeclaredAnnotation() : " + declaredAnnotation);
AnnotationParentTest declaredAnnotation1 = clazz.getDeclaredAnnotation(AnnotationParentTest.class);
System.out.println("getDeclaredAnnotation() : " + declaredAnnotation1);
Annotation[] declaredAnnotations = clazz.getDeclaredAnnotations();
for (Annotation annotation2 : declaredAnnotations) {
System.out.println("getDeclaredAnnotations() : " + annotation2);
}
}
}
测试结果
getAnnotation() : @com.type.AnnotationTest()
getAnnotation() : @com.type.AnnotationParentTest()
getAnnotations() : @com.type.AnnotationParentTest()
getAnnotations() : @com.type.AnnotationTest()
getDeclaredAnnotation() : @com.type.AnnotationTest()
getDeclaredAnnotation() : null
getDeclaredAnnotations() : @com.type.AnnotationTest()
2. 有关构造函数的几个方法
getConstructor()
getConstructors()
getDeclaredConstructor()
getDeclaredConstructors()
getConstructors() 与 getDeclaredConstructors() 的区别
getConstructors()只能获取public修饰的 构造函数,getDeclaredConstructors()能获取该类所有的构造函数,不管是public 还是private
@AnnotationTest
public class Clazz extends ClazzParent implements Serializable {
private int a;
private String name;
private Clazz() {
}
public Clazz(int a,String name) {
this.name = name;
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class ClazzTest {
public static void main(String[] args) {
Class<?> clazz = null;
try {
clazz = Class.forName("com.type.Clazz");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//只能拿到public的构造函数
Constructor<?>[] constructors = clazz.getConstructors();
for (Constructor constructor : constructors) {
System.out.println("getConstructors : " + constructor.getParameterCount());
}
Constructor<?>[] declaredConstructors = clazz.getDeclaredConstructors();
for (Constructor constructor : declaredConstructors) {
System.out.println("getDeclaredConstructors : " + constructor.getParameterCount());
}
}
}
测试结果
getConstructors : 2
getDeclaredConstructors : 0
getDeclaredConstructors : 2
如果类中没有构造函数,会有一个默认的public的构造函数
3.关于Field的几个方法
getField()
getFields() //返回某个类的所有(public)变量包括其继承类的公用变量
getDeclaredField() //所有的成员变量
getDeclaredFields()
public class Clazz {
private int a;
private String name;
protected String name1;
public String name2;
String name3;
}
测试结果
getFields : name2
getDeclaredFields : a
getDeclaredFields : name
getDeclaredFields : name1
getDeclaredFields : name2
getDeclaredFields : name3
4.关于Method的几个方法
getMethods() //getMethod(s):返回某个类的所有公用(public)方法包括其继承类的公用方法
getDeclaredMethods() //返回自身所有的方法,但不包括继承的方法
public class Clazz {
private int a;
private String name;
protected String name1;
public String name2;
String name3;
public int getA() {
return a;
}
public static void print(){
}
private static void print2(){
}
private void setA(int a) {
this.a = a;
}
protected String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
测试结果
getMethods : setName
getMethods : print
getMethods : getA
getMethods : wait
getMethods : wait
getMethods : wait
getMethods : equals
getMethods : toString
getMethods : hashCode
getMethods : getClass
getMethods : notify
getMethods : notifyAll
getDeclaredMethods : getName
getDeclaredMethods : setName
getDeclaredMethods : print
getDeclaredMethods : setA
getDeclaredMethods : getA
getDeclaredMethods : print2