java基础复习 - 9 - 枚举类、基本注解
作者:互联网
枚举
- 自己实现一个枚举类
public class Enumeration01 {
public static void main(String[] args) {
Season.SPRING.getName();
}
}
class Season {
private String name;
private String desc;
public static final Season SPRING = new Season("春天", "温暖");
public static final Season SUMMER = new Season("夏天", "炎热");
public static final Season AUTUMN = new Season("秋天", "凉爽");
public static final Season WINTER = new Season("冬天", "寒冷");
private Season(String name, String desc) {
this.name = name;
this.desc = desc;
}
public String getName() {
return this.name;
}
public String getDesc() {
return this.desc;
}
}
- 使用enum关键字
enum Season2 {
/*
1. 使用关键字enum代替class
2. public static final Season SPRING = new Season("春天", "温暖");直接使用SPRING("春天", "温暖")
3. 如果有多个对象,之间用,间隔即可
4. 如果使用enum来实现枚举,要求将定义常量对象卸载前面
*/
SPRING("春天", "温暖"),SUMMER("夏天", "炎热"),AUTUMN("秋天", "凉爽"),WINTER("冬天", "寒冷");
private String name;
private String desc;
// public static final Season SPRING = new Season("春天", "温暖");
// public static final Season SUMMER = new Season("夏天", "炎热");
// public static final Season AUTUMN = new Season("秋天", "凉爽");
// public static final Season WINTER = new Season("冬天", "寒冷");
private Season2(String name, String desc) {
this.name = name;
this.desc = desc;
}
public String getName() {
return this.name;
}
public String getDesc() {
return this.desc;
}
}
注意事项
- 当我们使用enum关键字开发一个枚举类时,默认会继承Enum类而且是一个final类
D:\own_code\vst\design-diagrams\target\classes\com\original\base\enumeration>javap Season2.class Compiled from "Enumeration01.java" final class com.original.base.enumeration.Season2 extends java.lang.Enum<com.original.base.enumeration.Season2> { public static final com.original.base.enumeration.Season2 SPRING; public static final com.original.base.enumeration.Season2 SUMMER; public static final com.original.base.enumeration.Season2 AUTUMN; public static final com.original.base.enumeration.Season2 WINTER; static {}; public java.lang.String getName(); public java.lang.String getDesc(); public static com.original.base.enumeration.Season2[] values(); public static com.original.base.enumeration.Season2 valueOf(java.lang.String); } D:\own_code\vst\design-diagrams\target\classes\com\original\base\enumeration>
- 传统的public static final Season2 SPRING = new Season2(“春天”,“温暖”);简化成SPRING(“春天”,“温暖”),这里必须知道,他调用的是哪个构造器
- 如果使用无参构造器创建枚举对象,则实参列表和小括号都可以省略
- 当有多个枚举对象时,使用,间隔最后以一个分号结尾
- 枚举对象必须放在枚举类的首行
// 下面代码会输出什么
enum Gender2{
BOY,GIRL;
}
Gender2 boy = Gender2.BOY;
Gender2 boy2 = Gender2.BOY;
System.out.println(boy); // 本质是调用Gender2的toString BOY
System.out.println(boy == boy2) // True
Enum常用方法
public static void main(String[] args) {
Season2 autumn = Season2.AUTUMN;
// 输出枚举对象的名称
System.out.println(autumn.name());// AUTUMN
// 该模具对象的次序/编号,从0开始编号的
System.out.println(autumn.ordinal()); // 2
// 从反编译可以看出 values方法,返回Season2[],含有定义的所有枚举对象
Season2[] seasons = Season2.values();
for (Season2 season2 : seasons) {
System.out.println(season2.getName());
}
// valueOf:将字符串转换成枚举对象,要求字符串必须为已有的常量名否则报异常
Season2 autumn2 = Season2.valueOf("AUTUMN");
// comparaTo 比较枚举常量的ordinal编号 AUTUMN编号减去WINTER编号
System.out.println(Season2.AUTUMN.compareTo(Season2.SUMMER)); // -1
}
注解
@Interface不是interface,是注解类在jdk1.5之后加入
三个基本的Annotation:
- @Override:限定某个方式是重写父类的方法,该注解只能用于方法,如果写了注解,编译器就回去检查该方法是否真的重写了父类的 public @interface Overide
- @Override标示指定重写父类的方法(从编译成眠验证),如果父类没有fly方法,则会报错
- 如果不写@Override注解,而父类仍然后,仍然构成重写
- 查看Override注解源码 @Targer(ElementType.METHOD),说明只能修饰方法
- @Targer是修饰注解的注解,成为元注解
- @Deprecated:用于标示某个程序元素(类、方法、字段、包、参数、构造器)已过时,即不推荐使用,但是任然可使用
- @SuppressWarnings:抑制编译器告警
元注解的基本介绍,JDK的元Annotation用于修饰其他Annotation,元注解本身作用不大
- Retention // 指定注解的作用范围,三种SOURCE,CLASS,RUNTIME
- RetentionPolicy.SOURCE:编译器使用后,直接丢弃这种策略的注释
- RetentionPolicy.CLASS:编译器默认把注解记录在class文件中。当运行Java程序时,JVM不会保留注解。这是默认值
- RetentionPolicy.RUNTIME:编译器将把注解记录在class文件中,当运行Java程序时,JVM会保留注解。程序可以通过反射获取该注解
- Target // 指定注解可以在哪些地方使用
- Document //指定该注解是否会在javadoc体现
- Inherited //子类会继承父类注解
标签:java,复习,Season,枚举,static,Season2,注解,public,String 来源: https://www.cnblogs.com/zyyanga/p/16065421.html