自定义注解属性

//新建一个接口 

@Retention(RetentionPolicy.RUNTIME)
public @interface DraweeViewAnnotation {
    String name() default "Hello world";
}

//写一个实体类

public class Enetiy {
    @DraweeViewAnnotation(name = "Hello world")
    String name;

    public Enetiy(String name) {
        this.name = name;
    }
    @DraweeViewAnnotation(name = "Hello world")
    public String getName() {
        return name;
    }

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

//获取name属性值

Class aClass = Class.forName("com.zjs.zhangjingshuai1203.Enetiy");
Method getName = aClass.getMethod("getName", null);
DraweeViewAnnotation annotation = getName.getAnnotation(DraweeViewAnnotation.class);
Toast.makeText(MainActivity.this, annotation.name()+"~", Toast.LENGTH_SHORT).show();

猜你喜欢

转载自blog.csdn.net/qq_42809182/article/details/84752416