Day11 JavaSE 枚举和注解

JavaSE 枚举和注解

一、枚举

1 定义

在某些情况下,一个类的对象是有限而且固定的,例如季节类,只有4个对象。如果一个一个单例模式,代码量太大,编译成本高,故使用枚举。

实现枚举类语句注意:

  • private修饰有参构造器
  • 属性使用private final修饰
  • 把该类的所有实例都使用public static final来修饰

2 自实现枚举类+实现接口

案例展示:(注意其中枚举类的实现语句)

package com.enumdemo;

public class Demo01 {
    public static void main(String[] args) {
        Season spring = Season.SPRING; //此处返回一个Season对象
        spring.showInfo();

        Season spring1 = Season.SPRING;

        //此处证明每次执行Season.SPRING获得相同对象,也就意味着枚举类中每个枚举都是单例的
        System.out.println(spring.equals(spring1));

        spring.test();
    }
}

enum Season implements ITest{
    SPRING("春天","春暖花开"), //此处调用有参私有构造,并传入参数
    SUMMER("夏天","炎炎夏日"),
    AUTUMN("秋天","秋高气爽"),
    WINTER("冬天","寒风凛冽");

    private final String name;
    private final String desc;

    private Season(String name, String desc){
        this.name = name;
        this.desc = desc;
    }
    public void showInfo(){
        System.out.println(this.name+": "+this.desc);
    }

    @Override
    public void test() {
        System.out.println("此处实现ITest接口的test方法");
    }
}

interface ITest{ //定义接口
    void test();
}

二、Annotation(注解)

1 Annotation概述

  • Annotation就是代码里的特殊标记,这些标记可以在编译,类加载,运行时被读取,并执行相应的处理。通过使用Annotation,程序员可以在不改变原有逻辑的情况下,在源文件中嵌入一些补充信息。
  • Annotation可以像修饰符一样被使用,可用于修饰包,类,构造器,方法,成员变量,参数,局部变量的声明,这些信息被保存在Annotation的“name=value”对中
  • Annotation能被用来为程序元素设置元数据

2 基本的Annotation

  • 使用Annotation时要在其前面增加@符号,并把该Annotation当成一个修饰符使用,用于修饰它支持的程序元素。

  • 三个基本的Annotation:

    • @Override:限定重写父类方法,该注释只能用于方法
    • @Deprecated:用于表示某个程序元素(类、方法等)已过时
    • @SuppressWarnings:抑制编译器警告

    上述注解展示:

    package com.annotation;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Demo01 {
        public static void main(String[] args) {
            new TestB().test1(); //方法过时
    
            @SuppressWarnings({"rawtypes","unused"}) //抑制警告,此时无泛型、未被使用均不出警告
            List list = new ArrayList();
        }
    }
    
    class TestA{
        public void test(){
    
        }
    }
    
    class TestB extends TestA{
        @Override  //注解表示方法重写
        public void test() {
            super.test();
        }
    
        @Deprecated  //注解表示方法过时
        public void test1(){
            System.out.println("DDD");
        }
    }
    

3 自定义Annotation

  • 定义新的Annotation类型使用@interface关键字
  • Annotation的成员变量在Annotation定义中以无参数方法的形式来声明。其方法名和返回值定义了该成员的名字和类型。
  • 可以在定义Annotation的成员变量时为其指定初始值,指定成员变量的初始值可以使用default关键字。
  • 没有成员定义的Annotation称为标记;包含成员变量的Annotation称为元数据Annotation。
package com.annotation;

import java.lang.annotation.*;

public class Demo02 {

}

class TestAA{
    @TestAnn(id=100,desc="姓名")
    String name;

}

//注解类
@Target(ElementType.FIELD)//这个注解类只能是给其他类的属性做注解
@Retention(RetentionPolicy.RUNTIME)//定义注解的生命周期,此时为整个运行周期
@Documented
@interface TestAnn{
    public int id() default 0;

    public String desc() default "";
}

写在最后

"I am the vine; you are the branches. If you remain in me and I in you, you will bear much fruit; apart from me you can do nothing. "

To Demut!

发布了32 篇原创文章 · 获赞 39 · 访问量 1735

猜你喜欢

转载自blog.csdn.net/qq_44958172/article/details/104705485