Java Annotation 可以帮助开发人员在编写代码的时候 提高效率,减少重复代码.但另一方面, 会造成代码维护难度的提升和效率的降低.
为方便自己的学习研究,做一下笔记.
1. 标注使用1
需要2个文件 标注文件和测试标注文件
public @interface TAnno {
}
public class testanno {
@Test
public void testAnnotation(){
test();
}
@TAnno hi h;
public void test(){
System.out.println(h.hey());
}
}
成功调用class hi,method hey. hey的方法需要是static的
2. 标注使用2
需要2个文件 标注文件和测试标注文件
public @interface TAnno {
}
public class testanno {
@Test
public void testAnnotation(){
test();
}
@TAnno(name="hello",ClassFactory=Hello.class)
public void test(){
Hello.h.sayHello();
}
}
public interface Hello {
public void sayHello();
static final Hello h=new hi();
}
成功调用,sayHello() 不需要为static方法
3. 标注使用3
public @interface TAnno {
}
public class testanno {
@Test
public void testAnnotation(){
test();
}
@TAnno (name="hello",ClassFactory=Hello.class)
public void test(){
System.out.println(Hello.hey());
}
}