java注解-Annotation

一、java简单注解
1.@SuppressWarnings("deprecation")
当调用某个已过时的方法时,尽管提示已过时,但我还是想调用的时候,就使用该注解
    
2.@Deprecated
当软件升级该show()方法已确定不再使用,但是为了照顾老用户还能使用该方法,又要提示新用户该方法软件最新版已弃用时:使用该注解
    
3.@Override
要重写/覆盖父类方法时候使用该注解,以确保覆盖的方法是否有写错


package net.dxs.day6;
  
public class AnnotationTest {
     //当调用某个已过时的方法时,尽管提示已过时,但我还是想调用的时候,就使用该注解
    @SuppressWarnings("deprecation")
    public static void main(String[] args) throws Exception {
        System.runFinalizersOnExit(true);
    }
  
     //当软件升级该show()方法已确定不再使用,但是为了照顾老用户还能使用该方法,又要提示新用户该方法软件最新版已弃用时:使用该注解
    @Deprecated
    public static void show() {
        System.out.println("Hello,深情小建!");
    }
 
      //要重写/覆盖父类方法时候使用该注解,以确保覆盖的方法是否有写错
    @Override
    public String toString() {
        return "Hello,World!";
    }
}

二、java自定义注解
1.定义在方法上并全部都赋值


package net.dxs.day6;
 
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
import net.dxs.day6.EnumTest.TrafficLamp;
 
// 生命周期
@Retention(RetentionPolicy.RUNTIME)
// 参数列表
@Target({ ElementType.METHOD, ElementType.TYPE, ElementType.FIELD,
        ElementType.PACKAGE })
public @interface ItcastAnnotation {
    int age = 22;//变量类型带初始值
 
    String color() default "blue";// 定义方法类型带默认值
 
    String value();// 方法类型不带默认值
     
    int[] arrayAttr() default {2,3,4};//数组类型带默认值
     
    EnumTest.TrafficLamp lamp() default TrafficLamp.RED;//枚举类型带默认值
 
    MetaAnnotation annotationAttr() default @MetaAnnotation("lijian");//注解类型带默认值
 
}

package net.dxs.day6;
 
public class AnnotationTest {
        @ItcastAnnotation(color = "red", value = "xyz", arrayAttr = { 1, 2, 3, 4 }, lamp = TrafficLamp.GREEN, annotationAttr = @MetaAnnotation("深情小建"))
    public static void main(String[] args) throws Exception {
        if (AnnotationTest.class.isAnnotationPresent(ItcastAnnotation.class)) {
                       ItcastAnnotation annotation = AnnotationTest.class.getMethod("main",String[].class).getAnnotation(ItcastAnnotation.class);
            System.out.println(annotation);
            System.out.println(ItcastAnnotation.age);
            System.out.println(annotation.color());
            System.out.println(annotation.value());
            System.out.println(annotation.arrayAttr().length);
            System.out.println(annotation.lamp().nextLamp());
            System.out.println(annotation.annotationAttr().value());
        } else {
            System.out.println("no...no...");
        }
    }
}
 
运行结果:
@net.dxs.day6.ItcastAnnotation(color=red, lamp=GREEN, annotationAttr=@net.dxs.day6.MetaAnnotation(value=深情小建), arrayAttr=[1, 2, 3, 4], value=xyz)
22
red
xyz
4
YELLOW
深情小建

附上枚举:

package net.dxs.day6;
 
public class EnumTest {
    public static void main(String[] args) {
        WeekDay weekDay = WeekDay.FRI;// second first first first first first first
        System.out.println(weekDay);// FRI
        System.out.println(weekDay.name());// FRI
        System.out.println(weekDay.ordinal());// 5
        System.out.println(WeekDay.valueOf("SUN").toString());// SUN
        System.out.println(WeekDay.values().length);// 7
 
        TrafficLamp tf = TrafficLamp.GREEN;
        System.out.println(tf.nextLamp());
    }
 
    public enum WeekDay {
        SUN(1), MON(), TUE, WED, THI, FRI, SAT;
        private WeekDay() {
            System.out.println("first");
        }
 
        private WeekDay(int day) {
            System.out.println("second");
        }
    }
 
    public enum TrafficLamp {
        RED(30) {
            public TrafficLamp nextLamp() {
                return GREEN;
            }
        },
        GREEN(45) {
            public TrafficLamp nextLamp() {
                return YELLOW;
            }
        },
        YELLOW(5) {
            public TrafficLamp nextLamp() {
                return RED;
            }
        };
        public abstract TrafficLamp nextLamp();
 
        private int time;
 
        private TrafficLamp(int time) {
            this.time = time;
        }
    }
 
}


附上注解:

package net.dxs.day6;
 
public @interface MetaAnnotation {
    String value();
}


2.定义在类上并使用默认值
package net.dxs.day6;
 
@ItcastAnnotation(value = "abc")
public class AnnotationTest {
    public static void main(String[] args) throws Exception {
        if (AnnotationTest.class.isAnnotationPresent(ItcastAnnotation.class)) {
            ItcastAnnotation annotation = AnnotationTest.class.getAnnotation(ItcastAnnotation.class);
            System.out.println(annotation);
            System.out.println(ItcastAnnotation.age);
            System.out.println(annotation.color());
            System.out.println(annotation.value());
            System.out.println(annotation.arrayAttr().length);
            System.out.println(annotation.lamp().nextLamp());
            System.out.println(annotation.annotationAttr().value());
        } else {
            System.out.println("no...no...");
        }
    }
}
运行结果:
@net.dxs.day6.ItcastAnnotation(color=blue, lamp=RED, annotationAttr=@net.dxs.day6.MetaAnnotation(value=lijian), arrayAttr=[2, 3, 4], value=abc)
22
blue
abc
3
GREEN
lijian


三、java自定义注解总结
注解相当于一种标记,在程序中加入注解就等于为程序打上了某种标记,没加则等于没有某种标记,以后java编译器开发工具和其他程序可以用反射来了解你的类以及各种元素上有无何种标记,看你有什么标记,就去做相应的事,标记可以加在包、类字段、方法、方法的参数以及局部变量上。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值