注解
类:要给一个类增强一些功能 继承,实现一个接口,还可以使用注解
可以通过使用注解 增强类,方法,属性的功能。
内置注解:
@Override :可以确保 重写的方法 的确存在于父类/接口中,可以有效的避免 单纯拼错等情况。
@Deprecated:给用于提示,该方法由于安全,性能问题等,已经不推荐使用了 此外,在版本升级时,
要计划删除一些方法,也通常会在前一个版本中
将该方法加上@Deprecated,然后再后续版本中删除。
@SuppressWarnings(value="unchecked") :压制警告(不建议使用) 忽略对泛型等的检查操作。
value值:unchecked,deprecation(忽略一些过期的API),
unused(是否未被使用),fallthrough(swtich是否一致往下 执行,而没有break),
path(忽略 对类路径不存在的检查),serialversionUID(忽略 一个类可以序列化,但却没有序列化的行为的警告),
all忽略以上所有情况。
package annontaion;
import annontaion.Fathe;
class Fathe
{
public void eat(){
System.out.println("father eat...");
}
}
class Son extends Fathe{
自定义注解: public @interface MyAnnotation { }
元注解:
元数据:修饰数据的数据
元注解:修饰注解的注解: @Targe,@Retention,@Document,@lnherited
@Target :限制注解 可以使用的位置
限制注解 能够使用哪些元素上(属性,方法,类);如果一个注解没有@Target描述,则该
注解可以修饰任何类型的元素;如果有@Target修饰,该注解就只能用于被@Target修饰的地方
哪些位置? ElementType.枚举
public enum ElementType {
/** Class, interface (including annotation type), or enum declaration */
TYPE,
/** Field declaration (includes enum constants) */
FIELD,
// 属性
/** Method declaration */
METHOD,
/** Formal parameter declaration */
PARAMETER,
/** Constructor declaration */
CONSTRUCTOR,
/** Local variable declaration */
LOCAL_VARIABLE,
/** Annotation type declaration */
ANNOTATION_TYPE,
/** Package declaration */
PACKAGE,
/**
* Type parameter declaration
*
* @since 1.8
*/
TYPE_PARAMETER,
/**
* Use of a type
*
* @since 1.8
*/
TYPE_USE
}
//自定义注解
package annontaion;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//修饰该注解 只能在 属性,方法上使用
package annontaion;
public class TestMyAnnotaiton
{
自定义注解如何使用?结合反射使用。
注解+反射 什么时候会真正使用?开发框架时 Spring mybatis springmvc
使用注解案例:
package annontaion;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//修饰该注解 只能在 属性,方法上使用
package annontaion;
import java.lang.annotation.Annotation;
//使用注解
public class TestMyAnnotation
{
@Document:
javadoc:java帮助文档。ABC.java-->帮助文档 默认情况下,javadoc不包含 注解的解释;
如果现在javadoc文档中 也包含对注解的说明,则需要使用@Document标注
@lnherited:继承
浙公网安备 33010602011771号