首页 > 代码库 > java注解

java注解

package com.ann.test;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;



@Target({ElementType.METHOD,ElementType.TYPE})// 作用域。注意首字母大写
/*
 * @Retention定义了该注解的生命周期
 */
@Retention(RetentionPolicy.RUNTIME)
/*
 * @Inherited阐述了某个被标注的类型是被继承的。可用于被注解类的子类
 */
@Inherited
/*
 * 可以被例如javadoc此类的工具文档化。Documented是一个标记注解,没有成员。
* 注意:生成javadoc的时候,不能有任何中文注释(英文注释没问题)
*/ @Documented public @interface Description { String value(); }

建一个Person接口

package com.ann.test;

public interface Person {

    public void name();
    public void age();
    @Deprecated // 方法过时的注解
    public void sing();
    
}
package com.ann.test;

@Description("This is class annotation")
public class Child implements Person {

    @Override
    @Description("This is method1 annotation")
    public void name() {
        // TODO Auto-generated method stub
        
    }

    @Override
    @Description("This is method2 annotation")
    public void age() {
        // TODO Auto-generated method stub
        
    }

    @Override
    @Description("This is method3 annotation")
    public void sing() {
        // TODO Auto-generated method stub
        
    }
    
}

 

更详细的解释请看这位大神的博客:http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html

java注解