• 反射_注解


    import java.lang.reflect.Constructor;
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
     
    public class Test01 {
    public static void main(String[] args) throws Exception {
     
    //反射最重要一步是拿到字节码文件。反射获取字节码的三种方式,一个类字节码文件只有一个
    //Person one = new Person();
    //Class c = one.getClass(); //有共有构造方法才行
    //Class c2 = Person.class;
    //Class c3 = Class.forName("Person"); //Class.forName()类的全路径,最常用方式,因为路径可以通过配置文件来获取
     
    Class c = Class.forName("com.zl.wu.Person");
    Constructor con = c.getDeclaredConstructor(String.class, int.class);  //有参构造类对象
    con.setAccessible(true);
    Person p = (Person) con.newInstance("fbb", 18);
    
    Field f = c.getDeclaredField("name");  //成员变量类对象
    f.setAccessible(true);
    f.set(p, "lbb");
    System.out.println(f.get(p));
    
    Method method = c.getDeclaredMethod("method", String.class, int.class);  //有参成员方法类对象
    method.setAccessible(true);
    String bb = (String) method.invoke(p, "bb", 20);
    System.out.println(bb);
    }
    }
     
    class Person{
    private String name;
    private int age;
     
    private Person() {
    System.out.println("无参构造");
    }
     
    private Person(String name, int age) {
    this.name = name;
    this.age = age;
    }
     
    private void method() {
    System.out.println("method");
    }
    private void method(String name,int age) {
    System.out.println(name+age);
    }
     
    }
     
    //测试单元
    import org.junit.Test;
     
    public class Test02 {
    public static void main(String[] args) {
    Test02 t = new Test02();
    t.method();
    }
    @Test//无形参,无返回值,非静态方法
    public void method() {
    System.out.println(666);
    }
    }
     
    //注解 annotation,是一种代码级别的说明,和类 接口平级关系
    /**
     * 自定义注解及赋值
     */
    
    public class Test03 {
        @Myano6(a = 1, b = "bb", c = Color.RED, d = @Myano5("hello"), e = String.class, f = {"fbb", "lbb"})
        public static void main(String[] args) {
        }
    }
    
    @interface Myano5 {
        String value();  //若只有一个属性且属性名为value,则 value= 可以省略
    }
    
    @interface Myano6 {
        int a() default 1;//基本类型,8种基本数据类型
    
        String b();//String
    
        Color c() default Color.BLUE;//枚举类型,可默认值
    
        Myano5 d();//注解类型
    
        Class e();//Class类型  
    
        String[] f();//一维数组类型
    }
    
    enum Color {
        RED,
        BLUE
    }
    
    /**
     * 解析注解
     */
    import java.lang.annotation.*;
    
    @Myano1(name = "fbb", age = 18)
    @Myano4("bb")
    public class Test01 {
        @Myano2(name = "lbb")
        //@Myano4("bb")  xx
        public static void main(String[] args) throws Exception {
            @Myano3("bb")
            int a;
            Class c = Class.forName("Test01");
            //Annotation myo1 = c.getDeclaredAnnotation(Myano1.class);  //得到自身的注解
            Myano1 myo1 = (Myano1) c.getAnnotation(Myano1.class);  //得到所有注解,包括父类的,强转为Myano1后可以直接点出属性来
            System.out.println(myo1);
            System.out.println(myo1.name());  //fbb  //强转为Myano1后才可以直接点出属性来,因属性只在具体的注解里
    
            Annotation[] annotations = c.getAnnotations();
            for (Annotation annotation : annotations) {
                System.out.println(annotation);
            }  //@Myano1(name=fbb, age=18)
                //@Myano4(value=bb)  没有Myano2和Myano3
    
        }
    }
    @Retention(RetentionPolicy.RUNTIME)  //以后一般都是runtime
    @interface Myano1 {
        String name();
        int age();
    }
    @Retention(RetentionPolicy.RUNTIME)
    @interface Myano2 {
        String name();
    }
    @Retention(RetentionPolicy.RUNTIME)
    @interface Myano3 {
        String value();
    }
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @interface Myano4 {
        String value();
    }
    
     
     
  • 相关阅读:
    Java_static
    Java_字符串操作
    Java_==
    Java_字符串
    Java_Random
    Java_Scanner
    杨辉三角
    颜色分类
    字符串倒序
    jQuery的基本事件
  • 原文地址:https://www.cnblogs.com/21556guo/p/13591650.html
Copyright © 2020-2023  润新知