JAVA反射(一)

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.

以上内容是JAVA中Class类源码的注释说明。大体意思是:(E文水平有限,见谅!)

Class的实例表示类和正在运行的Java应用程序中的接口。

1.枚举:是一种类、注释:是一种接口。

2.数组:也是一种类。具有相同维度的数组,指向的内存地址是相同的。

3.Java基本类型:boolean、byte、char、short、int、long、float、double以及关键字void表示为Class的对象

 

实例演示:

1. 新建一个UserBean测试类:

package cn.foxsand.day02;

public class UserBean {
    private int id;
    private String name;
    private int age;

    public UserBean() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

2. Class 测试类:

package cn.foxsand.day02;

/**
 * 测试java.lang.Class对象的获取方式
 */
public class Demo1 {
    public static void main(String[] args) {
        String path = "cn.foxsand.day02.UserBean";
        try {
            Class<?> clazz1 = Class.forName(path);

            //注意观察clazz2和clazz2打印出的hashCode
            System.out.println(clazz1);
            System.out.println(clazz1.hashCode());
            Class<?> clazz2 = Class.forName(path);
            System.out.println(clazz2.hashCode());

//   ---------------------         分割线      --------------------- 
            Class<?> strClazz1 = String.class;
            Class<?> strClazz2 = path.getClass();
            // 两个String类型的内存地址 == 返回结果
            System.out.println(strClazz1 == strClazz2);

            int[] arr01 = new int[10];
            int[] arr02 = new int[20];
            int[][] arr03 = new int[10][2];

            // 2个长度不同的一维数组和二维数组的hashCode
            System.out.println(arr01.getClass().hashCode());
            System.out.println(arr02.getClass().hashCode());
            System.out.println(arr03.getClass().hashCode());

        }catch (Exception e1){
            e1.printStackTrace();
        }
    }
}

 

3. 返回结果:

class cn.foxsand.day02.UserBean
1163157884
1163157884
true
1956725890
1956725890
356573597

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值