• Java注解和反射


    注解以及反射

    什么是注解:

    不是程序本身,但是可以被其他程序读取

    常用的内置注解

    • @Overwride 重写,子类重写父类的方法
    • @Deprecated 过时的,表示当前方法已经过时,不在推荐使用
    • @SuppressWarnings 抑制编译器警告,取消显示编译器警告

    元注解

    元注解是用于定义注解的注解
    包括@Retention、@Target、@Inherited、@Documented

    1. @Target:表示注解的作用范围
    2. @Retention:显示注解的生命周期,【source(源码)
    3. @Document:用来生成文档
    4. @Inherited:子类可以继承父类中的注解

    自定义注解

    @interface用来自定义注解

    注意

    • 注解只有成员变量,没有方法。
    • 注解里的属性形式为:参数类型+参数名()【default 默认值】
    @Target({ElementType.METHOD,ElementType.TYPE})
    @Inherited
    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @interface MyAnnotation {
        // 为注解定义属性
        String value();//书名
        double price() default 100;//价格,默认值为 100
        String[] authors();//多位作者
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    反射

    Java语言通过反射可以获得类似动态语言的特性
    动态语言就是在运行时可以改变其结构的语言

    JAVA反射机制

    是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。

    经典应用:AOP

    反射的优点

    有很大的灵活性,动态的创建对象

    反射的缺点

    使用反射创建对象执行慢于相同的不借助反射的操作

    类的获取方式

    Class c=Class.forName("Java.lang.String")//Class类自带的静态方法获取类
    Class c=String.class;//通过具体的类进行获取类
    Class c=string.getClass();//通过实例获取类
    
    • 1
    • 2
    • 3

    反射demo

    Person类:

    public class Person {
        private int num;
    
        public Person() {
        }
    
        public Person(int num){
            this.num=num;
        }
    
        public int getNum() {
            return num;
        }
    
        public void setNum(int num) {
            this.num = num;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    User类:

    public class User extends Person{
        private int no;
        private String name;
    
        public User(){
    
        }
    
        private User(int no, String name) {
            this.no = no;
            this.name = name;
        }
    
        private int getNo() {
            return no;
        }
    
         private void setNo(int no) {
            this.no = no;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    测试类:

    public class ReDemo {
        public static void main(String[] args) throws Exception {
            Class c=User.class;
            Field[]fields1=c.getFields();//获取本类的public属性
            Field[]fields2=c.getDeclaredFields();//获取本类的全部属性
            for(Field f:fields2){
                System.out.println(f);
            }
    
            Method[] methods1 = c.getMethods();//获取本类以及父类的public方法
            Method[] methods2 = c.getDeclaredMethods();//获取本类的全部方法
            for(Method m:methods2){
                System.out.println(m);
            }
    
            User user1 = (User)c.newInstance();//实例采取的是默认构造器
            Method setName=c.getDeclaredMethod("setName", String.class);
            setName.invoke(user1,"zjr");
            System.out.println(user1.getName());
    
            Constructor con=c.getDeclaredConstructor(int.class,String.class);
            con.setAccessible(true);//允许访问私有属性
            User user2 = (User)con.newInstance(1, "zjr");
            System.out.println(user2.getName());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
  • 相关阅读:
    python 为 网易云下载的 本地音乐文件增加 序号
    C语言之内存函数
    获取Steam余额的几种方式,及Steam余额被红锁的几种情况
    删除字符串字符,使输出结果不包含回文串
    jmeterbeanshell调用jsonpath获取对应值
    mysql 从入门到放弃— 数据库设计
    设计模式---建造者模式
    开源播放器GSYVideoPlayer的简单介绍及播放rtsp流的优化
    Linux常用命令——bmodinfo命令
    EIP-1559
  • 原文地址:https://blog.csdn.net/m0_58600477/article/details/126328006