首页 > 编程知识 正文

java怎么自定义异常,java开发中的注解自定义详解

时间:2023-05-03 18:53:11 阅读:33332 作者:4480

一.自定义评论类

@target(elementtype.method,ElementType.TYPE} ) )。

@ retention (retention policy.runtime ) )。

@Inherited //继承对接口没有帮助; 继承时只合并到类上面的注释中,而不被方法的注释继承

@Documented

公共@接口描述{

字符串值(;

}

1,@Target:注释的应用范围可以指定多个,其中传递一个数组;

2,@Retention :注释执行的生命周期:

3,@Inherited :子类是否可以继承。 但是,对于标记类,子类不继承此注释。

4,@Documented :生成的文档是否包含注释。

第二,测试自定义评论

@Description (这是Person接口) ) )

公共类人员{

(@description(Person-name ) ) ) ) ) ) ) ) ) ) 652

公共字符串name ((返回' aa ); (;

@description('person-age ) ) ) ) ) ) ) ) )。

public int age () {return 0; (;

@description('person-sing ' ) ) ) ) ) ) ) ) )。

公共语音();

}

/*@Description ('这是Child类') ) /

公共类池扩展人员{

/*@description('name ' ) /

公共字符串名称

返回空值;

}

/*@description('age ' ) /

公共int age () }

返回0;

}

/*@description('sing ' ) /

公共语音() }

}

}

要获取评论信息:

公共类珀斯Anno {

publicstaticvoidmain (字符串[ ] args ) throws ClassNotFoundException {

/*使用类加载器加载类*

classchild class=class.forname (shuitian.nai Luo.child );

//找到上面的评论

booleanispresent=child class.isannotation present (description.class );

if(ispresent ) {

//获得评论实例

描述d=(描述) childClass

. get annotation (description.class );

system.out.println(d.value ) );

}

//找到方法的评论

method [ ] methods=child class.getdeclaredmethods (;

方法MD :方法(for )//遍历此类方法

如果if (MD.isannotationpresent (description.class ) ) /方法具有description注释,则获取值信息

Escription d=(描述) md

. get annotation (description.class );

system.out.println(d.value ) );

}

}

system.out.println (------------- -高能分隔符------- ) );

/*如何评论另一种分析方法*

方法MD :方法(for )//遍历此类方法

annotation [ ] as=MD.get annotations (; //获取此方法的评论

for(annotationanno:as ) {

安诺防毒墙描述符(if ) {

描述d=(描述) anno;

system.out.println(d.value ) );

}

}

}

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。