首页 > 编程语言 >【Java SE】枚举类和注解

【Java SE】枚举类和注解

时间:2022-10-22 14:55:39浏览次数:59  
标签:Java String Season value 枚举 注解 public SE

1.枚举类的使用

当类的对象由有限个,确定的时候,我们称这种类为枚举类。当需要定义一组常量时,建议使用枚举类。而当枚举类中只有一个对象时,可以使用单例模式

1.1 enmu关键字

省略private finnal以及new Season部分:

enum Season {
 SPRING("春天", "春暖花开"),
 SUMMER("夏天", "热");
private final String seasonName;
private final String seasonDesc;


private Season(String seasonName, String seasonDesc) {
    this.seasonName = seasonName;
    this.seasonDesc = seasonDesc;
}


public String getSeasonName() {
    return seasonName;
}

public String getSeasonDesc() {
    return seasonDesc;
}

public static Season getSPRING() {
    return SPRING;
}

public static void setSPRING(Season SPRING) {
    Season.SPRING = SPRING;
}
}

1.2 自定义枚举类

    private final String seasonName;
    private final String seasonDesc;


    private Season(String seasonName, String seasonDesc) {
        this.seasonName = seasonName;
        this.seasonDesc = seasonDesc;
    }

    public static Season SPRING = new Season("春天", "春暖花开");

    public String getSeasonName() {
        return seasonName;
    }

    public String getSeasonDesc() {
        return seasonDesc;
    }

    public static Season getSPRING() {
        return SPRING;
    }

    public static void setSPRING(Season SPRING) {
        Seasoon.SPRING = SPRING;
    }
}

1.4 Enum类的主要方法

values() Season[] values = Season.values();查看类的所有枚举对象
valueOf(String objName) 返回对象名是objName的对象,如果没有则抛出IllegalArgumentException异常
toString() 返回当前枚举对象常量的名称

1.5 Enum关键字定义的枚举类实现接口

情况一:枚举类中实现接口中的抽象方法。

情况二:让枚举类的对象分别实现抽象方法。

SPRING("春天", "春暖花开"){
        void show() {
            System.out.println("Spring");
        }
    },
    SUMMER("夏天", "热") {
        @Override
        public void show() {

        }
    };

2.注解 Annotation

框架=注解 + 反射 + 设计模式

示例一:

![](file://E:\File\myNote\Java\枚举类和注解\1.png?msec=1666421005896)

示例二:在编译时进行格式检查(JDK内置的三个基本注解)

@ Override:限定重写

@ Deprecated:所修饰的元素(类、方法)已过时

@ SuppressWarnings:抑制编译器警告

如:SuppressWarnings("unused"), SuppressWarnings("rawtypes")

示例三:跟踪代码依赖性,实现替换配置文件功能

servlet3.0提供了注解,使得不需要在web.xml文件中进行servlet的部署。

自定义注解

@ interace 声明注解

public @interface  MyAnnotation {
    String value();
}
@goodsTest.MyAnnotation(value = "hello")
class Person {

} 


public @interface  MyAnnotation {
    String value() default "hello";
}

@goodsTest.MyAnnotation()
class Person {

}

内部使用成员只有一个时通常用value,可以指定成员的默认值,使用default定义。如果自定义注解没有成员,则表明注解只有标识作用。

jdk提供的元注解

修饰其他现有的Annotation注解进行解释说明的注解,需要加载注解的定义而不是声明处。

@Retention:用于指明Annotation的声明周期

@Retention(RetentionPolicy.RUNTIME)

包含一个RetentionPolicy类型的成员变量,使用Retention必须为该value指定值:

RetentionPolicy.SOURCE 源文件保留
RetentionPolicy.CLASS class保留,不会加载到内存中
RetentionPolicy.RUNTIME 运行时保留,只有声明为RUNTIME生命周期的注解,才能通过反射获取。

@Target:用于指定被修饰的Annotation能修饰哪些元素

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})

包含一个类型的成员变量,使用Retention必须为该value指定值:

value值 用于描述
CONSTRUCTOR 构造器
FIELD
LOCAL_VARIABLE 局部变量
METHOD 方法
PACKAGE
PARAMETER 参数
TYPE 类、接口(包括注解类型)或enum声明

@Documented:表示所修饰的注解被javadoc解析时会被保留到javadoc文档中,默认不保留。

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
public @interface Deprecated {
}

@Inherited:被修饰过的Annotation具有继承性。

3.jdk8注解的新特性

3.1 可重复注解

jdk8之前的写法:

public @interface MyAnnotation {
    String value() default "hello";
}
public @interface MyAnnotations {
    MyAnnotation[] value();
}
@MyAnnotations({@MyAnnotation(value = "hello"), @MyAnnotation(value = "hello")})

jdk8之后的写法:在MyAnnotation上声明@Repeatable,成员值为MyAnnotations.class。

@Repeatable(MyAnnotations.class)
public @interface MyAnnotation {
    String value();
}

3.2 类型注解

ElementType.TYPE_PARAMETER 该注解能写在类型变量的声明语句中(如泛型声明)。

ElementType.TYPE_USE 该注解能写在使用类型的任何语句中。

标签:Java,String,Season,value,枚举,注解,public,SE
From: https://www.cnblogs.com/tod4/p/16816086.html

相关文章

  • 【RocketMQ入门到精通】— RocketMQ学习入门指南 | RocketMQ服务发现(Name Server)精讲
    名言警句任何先进的技术均与魔法无异追本溯源​​经历了6个月的失踪,我将带着干货终究归来!【RocketMQ入门到精通】​​NameServer前提概要RocketMQ中,NameServers被设计用来......
  • 三角函数的另外三个伙伴—cot,sec,csc
    原文网址:https://zhuanlan.zhihu.com/p/162297688?utm_source=qq二.知识讲解1.认识另外三个小伙伴首先说cot我估计部分高中生应该学过cot。它叫余切,和tan互为倒数。所......
  • InfluxDB Cluster - InfluxDB Enterprise 集群的开源替代方案
    InfluxDBCluster-InfluxDBEnterprise集群的开源替代方案InfluxDBCluster-一个开源分布式时间序列数据库,InfluxDBEnterprise的开源替代方案GitHub:chengshiwen/......
  • 02,mybatise批量插入
    SqlSessionsession=sqlSessionFactory.openSession(ExecutorType.BATCH);try{SimpleTableMappermapper=session.getMapper(SimpleTableMapper.class);Listrecor......
  • 【C#】连接SQL Server 数据库
    usingSystem;usingSystem.Collections.Generic;usingSystem.Data.SqlClient;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceDb......
  • java线程的优先级
    packageA_ShangGuiGu.Thread.ThreadDemo;/***优先级*1.MAX_Priority-----最大优先级为10*2.MIN_Priority-----最小优先级为1*3.NORM_Priority----默认优先级5*4......
  • 基于Windows Server 2022系统-Exchange Server 2019基本安装
    1.准备一台WindowsServer2022系统,要首先安装以下软件:a..NETFramework4.8b.VisualC++RedistributablePackageforVisualStudio2012c.VisualC++Redistributab......
  • java基础-->注释
    注释注释:注释起到对代码标注和解释的作用,如果你去看看JDK源码,会发现他们有许多的注释,而且注释是比代码还要多的,可见为代码添加注释是非常重要的,写好注释能让别人更加容易......
  • java第六讲-StreamAndFileSourceCode
    1.PrintStream学习:Java打印流PrintStream类详解_风有点大的博客-CSDN博客_printstream类1.1向文件进行输出PrintStreamps=newPrintStream("helloworld01.txt"......
  • Windows Server **** Preview 通用密钥
    Microsoft还提供了WindowsServer****Preview(微软服务器Windows系统预览版本)密钥,但明确表示它们仅适用于预览版。服务器标准(standard):MFY9F-XBN2F-TYFMP-CCV49-......