在 Object类中定义了以下的方法,此方法将被所有子类继承
public final Class getClass();
以上的方法返回值的类型是一个Class类,此类是Java反射的源头,实际上所谓反射从程序的运行结果来看也很好理解,即:可以通过对象反射出类的名称。

对象照镜子后可以得到的信息:某个类的属性、方法和构造器、某个类到底实现类哪些接口。对于每个类而言,JRE都为其保留一个不变的 Class 类型的对象。一个 Class 对象包含了特定某个结构(class / interface / enum / annotation / primitive type / void / [])的有关信息。
Class clazz = Person.class;
Class clazz = person.getClass();
Class clazz = Class.forName("demo01.Student");
例子:
- package com.reflection;
-
- //测试class类的创建方式有哪些
- public class Test03 {
- public static void main(String[] args) throws ClassNotFoundException {
- Person person = new Student();
- System.out.println("这个人是:" + person.name);
-
- //方法一:通过对象获得
- Class c1 = person.getClass();
- System.out.println(c1.hashCode());
-
- //方法二:forname获得
- Class c2 = Class.forName("com.reflection.Student");
- System.out.println(c2.hashCode());
-
- //方法三:通过类名.class获得
- Class c3 = Student.class;
- System.out.println(c3.hashCode());
-
- //方法四:基本内置类型的包装类都有一个Type属性
- Class c4 = Integer.TYPE;
- System.out.println(c4);
-
- //获得父类类型
- Class c5 = c1.getSuperclass();
- System.out.println(c5);
-
- }
- }
-
- class Person {
- public String name;
-
- //构造器
- public Person() {
- }
-
- public Person(String name) {
- this.name = name;
- }
-
-
- @Override
- public String toString() {
- return "Person{" +
- "name='" + name + '\'' +
- '}';
- }
-
- }
-
- class Student extends Person {
- public Student() {
- this.name = "学生";
- }
- }
-
- class Teacher extends Person {
- public Teacher() {
- this.name = "老师";
- }
- }
输出结果:

例子:
- package com.reflection;
-
- import java.lang.annotation.ElementType;
-
- public class Test04 {
- public static void main(String[] args) {
- Class c1 = Object.class;
- Class c2 = Comparable.class;//接口
- Class c3 = String[].class;//一维数组
- Class c4 = int[][].class;//二维数组
- Class c5 = Override.class;//注解类型
- Class c6 = ElementType.class;//枚举类型
- Class c7 = Integer.class;//基本数据类型
- Class c8 = void.class;//void类型
- Class c9 = Class.class;//class类型
-
- System.out.println(c1);
- System.out.println(c2);
- System.out.println(c3);
- System.out.println(c4);
- System.out.println(c5);
- System.out.println(c6);
- System.out.println(c7);
- System.out.println(c8);
- System.out.println(c9);
-
-
- //数组类型一样,长度不一样,最后输出的hashCode是一样的
- //只要元素类型与维度一样,就是同一个Class.
- int[] a = new int[10];
- int[] b = new int[100];
- System.out.println(a.getClass().hashCode());
- System.out.println(b.getClass().hashCode());
-
- }
- }
输出结果:

| 方法名 | 功能说明 |
| static ClassforName(String name) | 返回指定类名 name 的 Calss 对象 |
| Object newInstance() | 调用缺省构造函数,返回Class对象的一个实例 |
| getName() | 返回此 Class 对象所表示的实体(类,接口,数组类或 void)的名称 |
| Class getSuperClass() | 返回当前 Class 对象的父类的 Class 对象 |
| Class[] getinterfaces() | 获取当前 Class对象的接口 |
| ClassLoader getClassLoader() | 返回该类的类加载器 |
| Constructor[] getConstructors() | 返回一个包含某些 Constructor 对象的数组 |
| Method getMothed(String name , Class...) | 返回一个 Method 对象,此对象的形参类型为 param Type |
| Field[] getDeclaredFields() | 返回 Field 对象的一个数组 |
枚举:
枚举常量是一种枚举类型中的值,及枚举值,枚举类型是由用户自定义的,只用用户在程序中定义它才能被使用。
创建一个枚举类型的基本语法:
enum 枚举类型名{
枚举值1,枚举值2,…
}
枚举类型名是由用户自定义的,同时枚举值通常都会用大写,多个枚举值之间用逗号隔开。
枚举优点:
枚举常用方法:
(1)values():把我们的枚举类型转换成数组
- public static void main(String[] args) {
- //将枚举类型转换成数组
- season[] arr1 = season.values();
- //利用for each循环进行打印
- for(season item : arr1)
- {
- System.out.println(item);
- }
- }
-
- public enum season{
- SPRING , SUMMER , AUTUMN , WINTER
- }
输出结果:
(2)ordinal():获得当前枚举类型的下标
- public static void main(String[] args) {
- //先将枚举类型转换成数组
- week[] arr = week.values();
- //循环遍历
- for(week item : arr)
- {
- //返回下标
- int index = item.ordinal();
- System.out.println(index);
- }
- }
- public enum week{
- MONDAY , TUESDAY , WEDNESDAY , THURSDAY , FRIDAY , SATURDAY , SUNDAY
- }
输出结果:
(3)valueOf():将一个字符串变成我们的枚举类型,若我们原本的枚举类型里不包含该字符串转换后的枚举值,那么系统会报错提示
- public static void main(String[] args) {
- //自定义字符串
- String str1 = "RED";
- //转换成已有的枚举类型
- Color color1 = Color.valueOf(str1);
- System.out.println(color1);
-
- //我们的枚举类里没有BLACK
- String str2 = "BLACK";
- //所以在这里的转换会产生错误
- Color color2 = Color.valueOf(str2);
- System.out.println(color2);
- }
- public enum Color{
- RED , GREEN , BLUE
- }
运行结果:
(4)compareTo():利用枚举的下标进行比较,得到下标相减得值
- public static void main(String[] args) {
- Color color1 = Color.RED;
- Color color2 = Color.GREEN;
- Color color3 = Color.BLUE;
-
- //比较RED和GREEN(0-1)
- int ret1 = color1.compareTo(color2);
- System.out.println(ret1);
- //比较RED和BLUE(0-2)
- int ret2 = color1.compareTo(color3);
- System.out.println(ret2);
- //比较GREEN和BLUE(1-2)
- int ret3 = color2.compareTo(color3);
- System.out.println(ret3);
- //比较BLUE和RED(2-0)
- int ret4 = color3.compareTo(color1);
- System.out.println(ret4);
- }
- public enum Color{
- RED , GREEN , BLUE
- }
运行结果: