温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java 反射-反射对象的获取

发布时间:2020-04-09 20:57:45 来源:网络 阅读:323 作者:wx5d21d5e6e5ab1 栏目:编程语言

java.lang.Class对象的获取方式

@SuppressWarnings("all") //压制警告
public class Deam {

public static void main(String[] args) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException  {

    try {
//一个类被加载后,jvm会创建一个对应类的Class对象,类的整个信息会放到对应的Class对象中
        Class c=Class.forName("cn.sxt.in.Demo");
        System.out.println(c);

        //类名.class 获取Class对象
        Class strc=String.class;
        //对象.getClass()获取Class对象
        String path="a";
        Class strc2=path.getClass();  //和上面的String的Class对象是同一个
        System.out.println(strc==strc2);  //true

        Class intc=int.class;  //获取int的Class对象

        int[] a=new int[10];
        int[] a2=new int[20];   //得到的数组的Class对象都是同一个
        String[] a3=new String[10];  //不同类型的数组和不同维度的数组得到的Class对象不同

        System.out.println(a.getClass().hashCode());
        System.out.println(a3.getClass().hashCode());

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

}

}
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI