Java 注解

原文地址:http://www.java3z.com/cwbwebhome/article/article8/83469.html?id=4418

在JAVA中存在三个JAVA内建的注解@Deprecated @Override @SuppressWarnings

@Deprecated表示不建议使用,可以用来为方法和类注解

一、把方法注解

package cn.yangtao.ceshi;

public class Student{
 public static void main(){
  
  sayHello();
 }
 @Deprecated
 private static void sayHello() {
  // TODO Auto-generated method stub
  
 }
}

在主方法中使用sayHello()时,编译时会出现警告

二、把类注解

package cn.yangtao.ceshi;
@Deprecated
class People{
 public static void onClass(){
  System.out.println("在类上添加注解");
 }
 
}

public class Student{
 public static void main(){
  
  sayHello();
  People.onClass();
 }
 @Deprecated
 private static void sayHello() {
  // TODO Auto-generated method stub
  
 }
}

引用People时,开发工具就会用横线来提醒你出错

@ Override用来标明是覆写

package cn.yangtao.ceshi;

class People{
 public void sayHello(){
  System.out.println("在类上添加注解");
 }
 
}

public class StudentDemo extends People{
 public static void main(String args[]){
  new StudentDemo().sayHello();
 }
 @ Override
 public void sayHello(){
  System.out.println("已经覆写");
 }
}

此时如果将sayHello()改为sayhello()就会出错,因为已经表明了该方法为覆写过的方法,所以方法名要一致

@ SuppressWarnings 用来压制警告

package cn.yangtao.ceshi;
public class StudentDemo {
 @SuppressWarnings("deprecation")
 public static void main(String args[]){
  
  new StudentDemo().sayHello();
 }
 @ Deprecated
 public void sayHello(){
  System.out.println("已经覆写");
 }
}

以下介绍SuppressWarnings的一些参数

1、deprecation 使用了不赞成使用的类或方法时的警告

2、unchecked 执行了未检查的转换时警告

3、fallthrough 当使用switch操作时case后未加入break操作,而导致程序继续执行其他case语句时出现的警告

4、path 当设置一个错误的类路径、源文件路径时出现的警告

5、serial 当在可序列化的类上缺少serialVersionUID定义时的警告

6、fianally 任何finally子句不能正常完成时警告

7、all 关于以上所有情况的警告

我们也可以定义自己的Annotation

package cn.yangtao.ceshi;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.CLASS)  ---------------------------->定义该注解的有效区域,还有SOURCE、RUNTIME
public @interface MyAnnotationDemo01 {
 String key();----------------------------------------------->用来接收传递进去的值
 String value();
}

使用MyAnnotationDemo01

package cn.yangtao.ceshi;
public class StudentDemo {
 @SuppressWarnings("deprecation")
 @MyAnnotationDemo01(key="hello",value="hello world")            这行是最关键的,记住赋值的方式
 public static void main(String args[]){
  
  new StudentDemo().sayHello();
 }
 @ Deprecated
 public void sayHello(){
  System.out.println("已经覆写");
 }
}
对于赋值可以有一下几种情况

1、数组型

package cn.yangtao.ceshi;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.CLASS)
public @interface MyAnnotationDemo01 {
 String[] key();
 
}
赋值方式

package cn.yangtao.ceshi;
public class StudentDemo {
 @SuppressWarnings("deprecation")
 @MyAnnotationDemo01(key={"hello","world"})
 public static void main(String args[]){
  
  new StudentDemo().sayHello();
 }
 @ Deprecated
 public void sayHello(){
  System.out.println("已经覆写");
 }
}

2、设置默认型

package cn.yangtao.ceshi;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.CLASS)
public @interface MyAnnotationDemo01 {
 String key() default "hello";
 
}

如果你使用该注解时没有给注解导入属性,则就会默认使用"hello"

3、可以用枚举来限制取值范围

定义枚举类

package cn.yangtao.ceshi;

public enum Color {
 Green,Red,Blue;
}
标明只能给注解传入枚举属性

package cn.yangtao.ceshi;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.CLASS)
public @interface MyAnnotationDemo01 {
 Color key() ;
 
}
使用注解

package cn.yangtao.ceshi;
public class StudentDemo {
 @SuppressWarnings("deprecation")
 @MyAnnotationDemo01(key=Color.Blue) 这个时候就只能取枚举中的对象,如果你有很高的控制欲这一招很好使
 public static void main(String args[]){
  
  new StudentDemo().sayHello();
 }
 @ Deprecated
 public void sayHello(){
  System.out.println("已经覆写");
 }
}

能够设置属性了,要怎么拿到和使用属性呢 ?

答案:反射找到注解所在的方法(如果忘记去看反射)------------>找到该方法上的注解

枚举

package cn.yangtao.ceshi;

public enum Color {
 Green,Red,Blue;
}

注解

package cn.yangtao.ceshi;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)     //  这个地方要设成运行时,否则在取属性的时候会出现空指向异常
public @interface MyAnnotationDemo01 {
 Color key() ;
}

取出useAnnotation方法上注解的属性

package cn.yangtao.ceshi;
public class StudentDemo {
 @SuppressWarnings("deprecation")
 public static void main(String args[]) throws SecurityException, NoSuchMethodException{
  useAnnotation();
  MyAnnotationDemo01 mt=StudentDemo.class.getMethod("useAnnotation", null).
  getAnnotation(MyAnnotationDemo01.class);
  Color c=mt.key();                                                    取出属性的值
  System.out.println(c.name());                                  获得枚举的对象的姓名
 }
 @MyAnnotationDemo01(key=Color.Blue)                  设置该属性的内容
 public  static void useAnnotation() {
  // TODO Auto-generated method stub
  
 }
 
}

枚举中元注解

概念:注解的注解就是元注解

常用的有:

@Target   声明被他标注的注解的作用区域比如 类、方法、属性、包等
@Documented  表示该注解可以被写到文档里面
@Inherited       声明该注解可以被继承

路慢慢其休远羲,吾将上下而求所
原文地址:https://www.cnblogs.com/garinzhang/p/2746406.html