Java-枚举类,注解
package com.lxl.java;/**
- 一、枚举类的使用
- 1. 枚举类的理解:类的对象只有有限个,确定的。我们称此类为枚举类
- 2. 当需要定义一组常量时,强烈建议使用枚举类
- 3. 如果枚举类中只有一个对象,则可以作为单例模式的实现方式。
- 二、如何定义枚举类
- 方式一:jdk5.0 之前,自定义枚举类
- 方式二:jdk5.0,可以使用 enum 关键字定义枚举类
- 三、Enum 类中的常用方法:
- values() 方法:返回枚举类型的对象数组。该方法可以很方便地遍历所有的枚举值。
- valueOf(String str):可以把一个字符串转为对应的枚举类对象。要求字符串必须是枚举类对象的“名字”。如不是,会有运行时异常:IllegalArgumentException。
- toString():返回当前枚举类对象常量的名称
- 四、使用 enum 关键字定义的枚举类实现接口的情况
- 情况一:实现接口,在 enum 类中实现抽象方法
- 情况二:让枚举类的对象分别实现接口中的抽象方法
*/
public class SeasonTest {</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) { Season spring </span>=<span style="color: rgba(0, 0, 0, 1)"> Season.SPRING; System.out.println(spring); }
}
//自定义枚举类
class Season{
//1. 声明 Season 对象的属性:private final 修饰
private final String seasonName;
private final String seasonDesc;</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2.私有化类的构造器,并给对象属性赋值</span> <span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Season(String seasonName,String seasonDesc){ </span><span style="color: rgba(0, 0, 255, 1)">this</span>.seasonName =<span style="color: rgba(0, 0, 0, 1)"> seasonName; </span><span style="color: rgba(0, 0, 255, 1)">this</span>.seasonDesc =<span style="color: rgba(0, 0, 0, 1)"> seasonDesc; } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">3.提供当前枚举类的多个对象:public static final的</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> Season SPRING = <span style="color: rgba(0, 0, 255, 1)">new</span> Season("春天","春暖花开"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> Season SUMMER = <span style="color: rgba(0, 0, 255, 1)">new</span> Season("夏天","夏日炎炎"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> Season AUTUMN = <span style="color: rgba(0, 0, 255, 1)">new</span> Season("秋天","秋高气爽"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> Season WINTER = <span style="color: rgba(0, 0, 255, 1)">new</span> Season("冬天","冰天雪地"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">4.其他诉求1:获取枚举类对象的属性</span> <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getSeasonName() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> seasonName; } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getSeasonDesc() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> seasonDesc; } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">4.其他诉求1:提供toString()</span>
@Override
public String toString() {
return "Season{" +
"seasonName='"+ seasonName +''' +
", seasonDesc='"+ seasonDesc +''' +
'}';
}
}
package com.lxl.java;/**
- 使用 enum 关键字定义枚举类
- 说明:定义的枚举类默认继承于 java.lang.Enum 类
*/
public class SeasonTest1 {
public static void main(String[] args) {
Season1 summer = Season1.SUMMER;
//toString(): 返回枚举类对象的名称
System.out.println(summer.toString());// System.out.println(Season1.class.getSuperclass());
System.out.println("************");
//values(): 返回所有的枚举类对象构成的数组
Season1[] values = Season1.values();
for(int i = 0;i < values.length;i++){
System.out.println(values[i]);
values[i].show();
}
System.out.println("************");
Thread.State[] values1 = Thread.State.values();
for (int i = 0; i < values1.length; i++) {
System.out.println(values1[i]);
}</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">valueOf(String objName):返回枚举类中对象名是objName的对象。</span> Season1 winter = Season1.valueOf("WINTER"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果没有objName的枚举类对象,则抛异常:IllegalArgumentException
// Season1 winter = Season1.valueOf("WINTER1");
System.out.println(winter);
winter.show();
}
}interface Info{
void show();
}//使用 enum 关键字枚举类
enum Season1 implements Info{
//1. 提供当前枚举类的对象,多个对象之间用 "," 隔开,末尾对象 ";" 结束
SPRING("春天","春暖花开"){
@Override
public void show() {
System.out.println("春天在哪里?");
}
},
SUMMER("夏天","夏日炎炎"){
@Override
public void show() {
System.out.println("宁夏");
}
},
AUTUMN("秋天","秋高气爽"){
@Override
public void show() {
System.out.println("秋天不回来");
}
},
WINTER("冬天","冰天雪地"){
@Override
public void show() {
System.out.println("大约在冬季");
}
};</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2.声明Season对象的属性:private final修饰</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">final</span><span style="color: rgba(0, 0, 0, 1)"> String seasonName; </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">final</span><span style="color: rgba(0, 0, 0, 1)"> String seasonDesc; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2.私有化类的构造器,并给对象属性赋值</span> <span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Season1(String seasonName,String seasonDesc){ </span><span style="color: rgba(0, 0, 255, 1)">this</span>.seasonName =<span style="color: rgba(0, 0, 0, 1)"> seasonName; </span><span style="color: rgba(0, 0, 255, 1)">this</span>.seasonDesc =<span style="color: rgba(0, 0, 0, 1)"> seasonDesc; } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">4.其他诉求1:获取枚举类对象的属性</span> <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getSeasonName() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> seasonName; } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getSeasonDesc() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> seasonDesc; }
// //4. 其他诉求 1:提供 toString()
//
// @Override
// public String toString() {
// return "Season1{" +
// "seasonName='"+ seasonName +''' +
// ", seasonDesc='"+ seasonDesc +''' +
// '}';
// }// @Override
// public void show() {
// System.out.println("这是一个季节");
// }
}
package com.lxl.java1;import org.junit.Test;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Date;/**
注解的使用
- 理解 Annotation:
① jdk 5.0 新增的功能
② Annotation 其实就是代码里的特殊标记, 这些标记可以在编译, 类加载, 运行时被读取, 并执行相应的处理。通过使用 Annotation,
程序员可以在不改变原有逻辑的情况下, 在源文件中嵌入一些补充信息。
③在 JavaSE 中,注解的使用目的比较简单,例如标记过时的功能,忽略警告等。在 JavaEE/Android
中注解占据了更重要的角色,例如用来配置应用程序的任何切面,代替 JavaEE 旧版中所遗留的繁冗
代码和 XML 配置等。
- Annocation 的使用示例
示例一:生成文档相关的注解
示例二:在编译时进行格式检查 (JDK 内置的三个基本注解)
@Override: 限定重写父类方法, 该注解只能用于方法
@Deprecated: 用于表示所修饰的元素 (类, 方法等) 已过时。通常是因为所修饰的结构危险或存在更好的选择
@SuppressWarnings: 抑制编译器警告示例三:跟踪代码依赖性,实现替代配置文件功能
- 如何自定义注解:参照 @SuppressWarnings 定义
- ① 注解声明为:@interface
- ② 内部定义成员,通常使用 value 表示
- ③ 可以指定成员的默认值,使用 default 定义
- ④ 如果自定义注解没有成员,表明是一个标识作用。
如果注解有成员,在使用注解时,需要指明成员的值。
自定义注解必须配上注解的信息处理流程 (使用反射) 才有意义。
自定义注解通过都会指明两个元注解:Retention、Target
- jdk 提供的 4 种元注解
元注解:对现有的注解进行解释说明的注解
Retention:指定所修饰的 Annotation 的生命周期:SOURCE\CLASS(默认行为)\RUNTIME
只有声明为 RUNTIME 生命周期的注解,才能通过反射获取。
Target: 用于指定被修饰的 Annotation 能用于修饰哪些程序元素
出现的频率较低
Documented: 表示所修饰的注解在被 javadoc 解析时,保留下来。
Inherited: 被它修饰的 Annotation 将具有继承性。5. 通过反射获取注解信息 --- 到反射内容时系统讲解
- jdk 8 中注解的新特性:可重复注解、类型注解
6.1 可重复注解:① 在 MyAnnotation 上声明 @Repeatable,成员值为 MyAnnotations.class
② MyAnnotation 的 Target 和 Retention 等元注解与 MyAnnotations 相同。6.2 类型注解:
ElementType.TYPE_PARAMETER 表示该注解能写在类型变量的声明语句中(如:泛型声明)。
ElementType.TYPE_USE 表示该注解能写在使用类型的任何语句中。
*/
public class AnnotationTest {</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) { Person p </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Student(); p.walk(); Date date </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Date(2020, 10, 11<span style="color: rgba(0, 0, 0, 1)">); System.out.println(date); @SuppressWarnings(</span>"unused"<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">int</span> num = 10<span style="color: rgba(0, 0, 0, 1)">;
// System.out.println(num);
@SuppressWarnings({ "unused", "rawtypes" })
ArrayList list = new ArrayList();
}@Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testGetAnnotation(){ Class clazz </span>= Student.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">; Annotation[] annotations </span>=<span style="color: rgba(0, 0, 0, 1)"> clazz.getAnnotations(); </span><span style="color: rgba(0, 0, 255, 1)">for</span>(<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0;i < annotations.length;i++<span style="color: rgba(0, 0, 0, 1)">){ System.out.println(annotations[i]); } }
}
//jdk 8 之前的写法:
//@MyAnnotations({@MyAnnotation(value="hi"),@MyAnnotation(value="hi")})
@MyAnnotation(value="hi")
@MyAnnotation(value="abc")
class Person{
private String name;
private int age;</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Person() { } @MyAnnotation </span><span style="color: rgba(0, 0, 255, 1)">public</span> Person(String name, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> age) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.name =<span style="color: rgba(0, 0, 0, 1)"> name; </span><span style="color: rgba(0, 0, 255, 1)">this</span>.age =<span style="color: rgba(0, 0, 0, 1)"> age; } @MyAnnotation </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> walk(){ System.out.println(</span>"人走路"<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> eat(){ System.out.println(</span>"人吃饭"<span style="color: rgba(0, 0, 0, 1)">); }
}
interface Info{
void show();
}class Student extends Person implements Info{
@Override </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> walk() { System.out.println(</span>"学生走路"<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> show() { }
}
class Generic<@MyAnnotation T>{
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> show() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> @MyAnnotation RuntimeException{ ArrayList</span><@MyAnnotation String> list = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<><span style="color: rgba(0, 0, 0, 1)">(); </span><span style="color: rgba(0, 0, 255, 1)">int</span> num = (@MyAnnotation <span style="color: rgba(0, 0, 255, 1)">int</span>) 10L<span style="color: rgba(0, 0, 0, 1)">; }
}
package com.lxl.java1;import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;import static java.lang.annotation.ElementType.*;
/**
*
*/
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
public @interface MyAnnotations {MyAnnotation[] value();
}
package com.lxl.java1;import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*;
/**
*/
@Inherited
@Repeatable(MyAnnotations.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE,TYPE_PARAMETER,TYPE_USE})
public @interface MyAnnotation {String value() </span><span style="color: rgba(0, 0, 255, 1)">default</span> "hello"<span style="color: rgba(0, 0, 0, 1)">;
}