java注解入门和用法举例
java注解是一种特殊的类型,依托java虚拟机的支持,可以对代码进行动态的声明或管理。
—-系统内建的三种Annotation:
java.lang: Annotation类型:
1. public @interface Deprecated
A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists. Compilers warn when a deprecated program element is used or overridden in non-deprecated code.
2.public @interface Override
Indicates that a method declaration is intended to override a method declaration in a superclass. If a method is annotated with this annotation type but does not override a superclass method, compilers are required to generate an error message.
1. public @interface SuppressWarnings
Indicates that the named compiler warnings should be suppressed in the annotated element (and in all program elements contained in the annotated element). Note that the set of warnings suppressed in a given element is a superset of the warnings suppressed in all containing elements. For example, if you annotate a class to suppress one warning and annotate a method to suppress another, both warnings will be suppressed in the method.
//方法:
public abstract String[] value; // 接收一个String表示警告信息种类数组!
–1.@Override:覆写注释;
package com.mldn;
public class AnnotationDemo
{
public static void main(String[] args)
{
Person per = new Student();
per.getInfo();
}
}
class Person
{
public void getInfo()
{
}
}
class Student extends Person
{
@Override // 保证覆写的方法不会出错:表示这是个覆写!
public void gtInfo()
{
System.out.println(“覆写方法调用成功!”);
}
}
/*
ubuntu@xu-desktop:~$ javac -d . work/AnnotationDemo.java
ubuntu@xu-desktop:~$ java com.mldn.AnnotationDemo
覆写方法调用成功!
ubuntu@xu-desktop:~$ javac -d . work/AnnotationDemo.java
work/AnnotationDemo.java:22: 方法不会覆盖或实现超类型的方法
@Override // 保证覆写的方法不会出错:表示这是个覆写!
^
1 错误 // 覆写出错时!
*/
—2.@Deprecated:不建议使用的 注释;
–可以表示不建议使用的方法接口:
package com.mldn;
public class AnnotationDemo1
{
public static void main(String[] args)
{
new Student().getInfo();
}
}
class Student
{
@Deprecated // 不建议使用的方法!
public void getInfo()
{
System.out.println(“覆写方法调用成功!”);
}
}
/*
ubuntu@xu-desktop:~$ javac -d . work/AnnotationDemo1.java
注意:work/AnnotationDemo1.java 使用或覆盖了已过时的 API。
注意:要了解详细信息,请使用 -Xlint:deprecation 重新编译。
*/
—-可以表示不建议使用的类:
package com.mldn;
public class AnnotationDemo2
{
public static void main(String[] args)
{
new Student().getInfo();
}
}
@Deprecated // 表示不建议使用的类
class Student
{
public void getInfo()
{
System.out.println(“覆写方法调用成功!”);
}
}
/*
ubuntu@xu-desktop:~$ javac -d . work/AnnotationDemo2.java
注意:work/AnnotationDemo2.java 使用或覆盖了已过时的 API。
注意:要了解详细信息,请使用 -Xlint:deprecation 重新编译。
ubuntu@xu-desktop:~$ javac -Xlint:deprecation -d . work/AnnotationDemo2.java
work/AnnotationDemo2.java:7: 警告:[deprecation] com.mldn 中的 com.mldn.Student 已过时
new Student().getInfo();
^
1 警告
*/
—3.@SupperessWarnings 压制警告信息:
package com.mldn;
public class AnnotationDemo3
{
@SuppressWarnings (value = {“unchecked”,”deprecation”}) // 该注释 将压制本类中运行时类型为:”unchecked”所有警告信息的产生!
public static void main(String[] args)
{
Demo d = new Demo(); // 未对泛型指定类型!
d.setVar(“张三”);
}
}
@Deprecated // 不建议使用,如果使用将报警
class Demo
{
private T var;
public void setVar(T var)
{
this.var = var;
}
}
/*
ubuntu@xu-desktop:~$ javac -d . work/AnnotationDemo3.java
注意:work/AnnotationDemo3.java 使用了未经检查或不安全的操作。
注意:要了解详细信息,请使用 -Xlint:unchecked 重新编译。 // 未对泛型进行确定类型,报警!,可以压制这个警告信息!
ubuntu@xu-desktop:~$ javac -d . work/AnnotationDemo3.java
注意:work/AnnotationDemo3.java 使用或覆盖了已过时的 API。 // 说明还需要压制信息:”deprecation”
注意:要了解详细信息,请使用 -Xlint:deprecation 重新编译。
*/
———-自定义注释:
格式:
public @interface AnnotationName
{
public 修饰符 变量类型 变量名();
}
–1.自定义注释的简单实例:
package com.mldn;
public class AnnotationDemo4
{
@MyAnnotation // 使用自定义注释
public static void main(String[] args)
{
}
}
@interface MyAnnotation // 自定义注释
{
}
—2.含有变量的自定义注释:
package com.mldn;
public class AnnotationDemo4
{
@MyAnnotation(“张三”) // 使用自定义注释
public static void main(String[] args)
{
}
}
@interface MyAnnotation // 自定义注释
{
public String value();
}
—-3.含有多个变量的自定义注释:
package com.mldn;
public class AnnotationDemo4
{
@MyAnnotation(key = “001”, value = “张三”) // 使用自定义注释
public static void main(String[] args)
{
}
}
@interface MyAnnotation // 自定义注释
{
public abstract String value(); // 定义预设值
public String key();
}
—–4.含有数组的自定义注释:
package com.mldn;
public class AnnotationDemo4
{
@MyAnnotation(key = {“001”}) // 使用自定义注释
public static void main(String[] args)
{
}
}
@interface MyAnnotation // 自定义注释
{
public String[] key();
}
—-5.设置Annotation默认值:
package com.mldn;
@MyAnnotation
public class AnnotationDemo4
{
@MyAnnotation(key = 1) // 使用自定义注释
public static void main(String[] args)
{
}
}
@interface MyAnnotation // 自定义注释
{
public int[] key() default {1}; // 用defalut关键字指定默认值
}
—–6.使用枚举类型定义注释变量:
package com.mldn;
@MyAnnotation
public class AnnotationDemo4
{
@MyAnnotation(key = MyName.China) // 使用自定义注释
public static void main(String[] args)
{
}
}
@interface MyAnnotation // 自定义注释
{
public MyName key() default MyName.k187; // 默认值为枚举类型,并且指定默认枚举值
}
enum MyName
{
k187, China, Jinan; // 定义枚举
}
声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 嗅谱网
转载请注明:转自《java注解入门和用法举例》
本文地址:http://www.xiupu.net/archives-147.html
关注公众号:
微信赞赏
支付宝赞赏