自定義註解屬性
阿新 • • 發佈:2018-12-03
//新建一個介面
@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();