我是一个带注释界面的新手:
@interface
Test {
public String getInfo() default "hi";
}
@test
class TestImpl implements Test
{
public String getInfo(){return getInfo();}
public static void main(String...args)
{
TestImpl impl=new TestImpl ();
impl.getInfo();
}
}
实际上,我对此完全感到困惑,我想打印我的getInfo()方法的默认值。并且不知道如何使用它以及带注释的界面的优点。
如果某个伙伴对此有所了解,请将上述代码更改为默认值的可打印格式,如果有可能,则给我一个URL,从中我可以阅读更多关于注释界面的内容。 / p>
谢谢, Subodh Ray
答案 0 :(得分:4)
class TestImpl { // not implementing the annotation interface
然后:
Test annotation = TestImpl.class.getAnnotation(Test.class);
String info = annotation.getInfo();
请注意,通常注释属性未定义为getter。所以info()
代替getInfo()
更新:您的注释课程需要@Retention(RetentionPolicy.RUNTIME)
。如果不存在,则注释不会在运行时保留。