Java基础复习 Day 28

Java基础复习 Day 28

注解与反射 2

1. JavaClass对象的方法

通过class对象可以获得当前类的属性,方法,类名,注解,构造方法,父类等所有类的信息都可以获取

public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
    Class c1 = Class.forName("com.kou.reflection.User");
    //获得包名+类名
    System.out.println(c1.getName());//com.kou.reflection.User
    //获得类名
    System.out.println(c1.getSimpleName());//User
    Field[] fields = c1.getFields();//获得累的public属性
    //获得所有属性
    for (Field field : c1.getDeclaredFields()) {
        System.out.println(field);
        //private java.lang.String com.kou.reflection.User.name
        //private int com.kou.reflection.User.id
        //private int com.kou.reflection.User.age
    }

    //获得指定属性的值
    Field name = c1.getDeclaredField("name");
    System.out.println(name);//private java.lang.String com.kou.reflection.User.name
    for (Method method : c1.getDeclaredMethods()) {
        System.out.println(method);
        //获得本类的public方法
        //public java.lang.String com.kou.reflection.User.getName()
        //public int com.kou.reflection.User.getId()
        //public void com.kou.reflection.User.setName(java.lang.String)
        //public void com.kou.reflection.User.setId(int)
        //public void com.kou.reflection.User.setAge(int)
        //public int com.kou.reflection.User.getAge()
    }
    System.out.println("=======================================");
    for (Method method : c1.getMethods()) {
        System.out.println(method);
        //获得本类和父类的所有方法
        //public java.lang.String com.kou.reflection.User.getName()
        //public int com.kou.reflection.User.getId()
        //public void com.kou.reflection.User.setName(java.lang.String)
        //public void com.kou.reflection.User.setId(int)
        //public void com.kou.reflection.User.setAge(int)
        //public int com.kou.reflection.User.getAge()
        //public final void java.lang.Object.wait() throws java.lang.InterruptedException
        //public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
        //public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
        //public boolean java.lang.Object.equals(java.lang.Object)
        //public java.lang.String java.lang.Object.toString()
        //public native int java.lang.Object.hashCode()
        //public final native java.lang.Class java.lang.Object.getClass()
        //public final native void java.lang.Object.notify()
        //public final native void java.lang.Object.notifyAll()
    }

}

2.Class对象的意义
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

//动态创建对象
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {

    Class c1 = Class.forName("com.kou.reflection.User");
    //通过newInstance构造一个对象
    User user = (User) c1.newInstance();//相当于调用了user的无参构造方法
    System.out.println(user);//User{name='null', id=0, age=0}
    //通过获得构造器获取一个对象
    Constructor constructor = c1.getDeclaredConstructor(String.class, int.class, int.class);
    User user2 = (User) constructor.newInstance("Karen",223,23);
    System.out.println(user2);//User{name='Karen', id=223, age=23}
    //通过反射操作普通方法
    Method m1 = c1.getDeclaredMethod("setName", String.class);
    m1.invoke(user2,"Karen");
    System.out.println(user2.getName());//Karen
    //通过反射操作属性
    Field name = c1.getDeclaredField("name");
    //想要访问私有属性需要将安全检查关掉
    name.setAccessible(true);//不管下边输出语句的异常://Exception in thread "main" java.lang.IllegalAccessException: Class com.kou.reflection.Test09 can not access a member of class com.kou.reflection.User with modifiers "private"
    name.set(user2,"Kyle");
    System.out.println(user2);//User{name='Kyle', id=223, age=23}

}
  1. 性能分析

    //分析性能
    public class Test10 {
        public static void main(String[] args) throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
            test1();//普通new对象需要调用: 16 ms
            test2();//反射方式并且不关闭安全检查需要调用: 1704 ms
            test3();//反射方式关闭安全检查需要调用: 1008 ms
    
        }
        //普通方式调用
        public static void test1(){
            User user = new User();
            long startTime= System.currentTimeMillis();
            for (int i = 0; i < 1000000000; i++) {
                user.getName();
            }
            long endTime = System.currentTimeMillis();
            System.out.println("普通new对象需要调用: " + (endTime - startTime) + " ms");
        }
        //反射方式不关闭安全检查调用
        public static void test2() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
            Class c1 = Class.forName("com.kou.reflection.User");
            User u1 = (User) c1.newInstance();
            Method getName = c1.getDeclaredMethod("getName",null);
            long startTime= System.currentTimeMillis();
            for (int i = 0; i < 1000000000; i++) {
                getName.invoke(u1,null);
            }
            long endTime = System.currentTimeMillis();
            System.out.println("反射方式并且不关闭安全检查需要调用: " + (endTime - startTime) + " ms");
        }
        //反射方式调用关闭安全检查
        public static void test3() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
            Class c1 = Class.forName("com.kou.reflection.User");
            User u1 = (User) c1.newInstance();
            Method getName = c1.getDeclaredMethod("getName", null);
            getName.setAccessible(true);
            long startTime= System.currentTimeMillis();
            for (int i = 0; i < 1000000000; i++) {
                getName.invoke(u1,null);
            }
            long endTime = System.currentTimeMillis();
            System.out.println("反射方式关闭安全检查需要调用: " + (endTime - startTime) + " ms");
        }
    }
    

    本篇课程来源均是狂神说,图片来自于bilibili的视频截图,如有侵权请务必联系。
    B站视频地址请点击这里

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值