@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,ElementType.METHOD})
@Inherited
public @interface AnnoInherited {
}
public class TestAnnoInherated {
public static void main(String[] args) {
Annotation[] annos=new Goo().getClass().getAnnotations();
for(Annotation a:annos){
System.out.println(a);
}
}
}
@AnnoInherited
class Foo{
}
class Goo extends Foo{
}
package test.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AnimalInfo {
String shout() default "";
//能不能看门
boolean isGuard() default true;
}
public class TestAnimalInfo {
public static void main(String[] args) {
Animal animal=new Animal();
AnimalInfo info=animal.getClass().getAnnotation(AnimalInfo.class);
if(info!=null){
Annotation anno=info;//此处并没有报错.Annotation是一个接口.info是一个注解.这是因为编译器会将注解编译成接口,并且继承了Annotation
System.out.println("Annotation类信息:"+Annotation.class);
System.out.println("AnimalInfo类信息:"+AnimalInfo.class);
Class[] cs=AnimalInfo.class.getInterfaces();
for(Class c:cs){
System.out.println(c); //AnimalInfo编译后就是一个接口,并且继承了Annotation,这里得到了证实.
}
System.out.println("info对象的类信息:"+info.getClass());
if("wangwang".equals(info.shout())&&info.isGuard()){
System.out.println("the animal is a dog");
}else if("miaomiao".equals(info.shout())&&!info.isGuard()){
System.out.println("the animal is a cat");
}else{
System.out.println("the animal is not a dog or cat");
}
}else{
System.out.println("it's not a animal");
}
}
}
@AnimalInfo(shout="wangwang",isGuard=true)
class Animal{
}
Annotation类信息:interface java.lang.annotation.Annotation AnimalInfo类信息:interface test.annotation.AnimalInfo interface java.lang.annotation.Annotation info对象的类信息:class com.sun.proxy.$Proxy1 the animal is a dog
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有