引出:
分析:
解决方案:枚举
package com.bijing.enum_;
/**
* @author 毕晶
* @date 2022/6/2 10:10 AM
*/
public class Enumeration01 {
public static void main(String[] args) {
System.out.println(Season.AUTUMN);
}
}
class Season {
public static final Season SPRING = new Season("春天", "温暖");
public static final Season WINTER = new Season("冬天", "寒冷");
public static final Season AUTUMN = new Season("秋天", "凉爽");
public static final Season SUMMER = new Season("夏天", "炎热");
private String name;
private String desc;
// 1.私有化构造器
// 2.去掉set方法
// 3.在Season内部直接创建固定对象
// 4.加入final修饰符,final修饰的对象只是引用值不可更改,但是它里面的属性可以通过set进行更改
private Season(String name, String desc) {
this.name = name;
this.desc = desc;
}
public String getName() {
return name;
}
public String getDesc() {
return desc;
}
@Override
public String toString() {
return "Season{" +
"name='" + name + '\'' +
", desc='" + desc + '\'' +
'}';
}
}
// 1.使用关键字 enum替代class
// 2.public static final Season02 SPRING = new Season02("春天", "温暖");直接使用
// SPRING("春天","温暖");代码要在属性上面写
// 3.有多个常量(对象),使用,间隔即可
// 4.如果使用enum来实现枚举,要把定义的常量对象写在最前面
// 5.如果我们使用的是无参构造器,创建常量对象,则可以省略(〕
enum Season02 {
SPRING("春天", "温暖"), SUMMER("夏天", "炎热");
/* public static final Season02 SPRING = new Season02("春天", "温暖");
public static final Season02 WINTER = new Season02("冬天", "寒冷");
public static final Season02 AUTUMN = new Season02("秋天", "凉爽");
public static final Season02 SUMMER = new Season02("夏天", "炎热");*/
private String name;
private String desc;
private Season02(String name, String desc) {
this.name = name;
this.desc = desc;
}
public String getName() {
return name;
}
public String getDesc() {
return desc;
}
@Override
public String toString() {
return "Season02{" +
"name='" + name + '\'' +
", desc='" + desc + '\'' +
'}';
}
}
6.枚举的toString()方法返回的是name
enum继承了Enum
Season02 season = Season02.SPRING;
//name()方法输出常量的名称,即SPRING
System.out.println(season.name());
//ordinal()方法输出的是该枚举对象的次序/编号 ,下标从0开始
System.out.println(season.ordinal());
// values()方法返回一个season02数组
Season02[] seasons = Season02.values();
for (Season02 season02 : seasons) {
System.out.println(season02);
}
//valueOf() 将字符串转换成枚举对象,要求字符串必须为己有的常量名,否则报异常
//1.根据你输入的 "AUTUMN" 到 Season2的枚举对象去查找
//2.如果找到了,就返回,如果没有找到,就报错
Season02 autumn1 = Season02.valueOf("AUTUMN");
System.out.println("autumn1=" + autumn1);
System.out.println(Season02.AUTUMN == autumn1);
//compareTo()方法比较的是两个枚举常量,比较它们的编号
//返回SPRING的编号 - AUTUMN的编号
System.out.println(Season02.SPRING.compareTo(Season02.AUTUMN));
}
/**
* @author 毕晶
* @date 2022/6/2 1:12 PM
*/
enum Spring implements Season03 {
SPRING();
@Override
public void show() {
System.out.println("我是春天");
}
}
interface Season03 {
void show();
}
public class EnumDetail {
public static void main(String[] args) {
Spring.SPRING.show();
System.out.println(Spring.SPRING);
}
}
主要是语法校验看你是否真的重写了
Override使用说明
关键字 | 用途 |
---|---|
all | to suppress all warnings (抑制所有警告) |
boxing | to suppress warnings relative to boxing/unboxing operations (抑制装箱、拆箱操作时候的警告) |
cast | to suppress warnings relative to cast operations (抑制映射相关的警告) |
dep-ann | to suppress warnings relative to deprecated annotation (抑制启用注释的警告) |
deprecation | to suppress warnings relative to deprecation (抑制过期方法警告) |
fallthrough | to suppress warnings relative to missing breaks in switch statements (抑制确在switch中缺失breaks的警告) |
finally | to suppress warnings relative to finally block that don’t return (抑制finally模块没有返回的警告) |
hiding | to suppress warnings relative to locals that hide variable(抑制相对于隐藏变量的局部变量的警告) |
incomplete-switch | to suppress warnings relative to missing entries in a switch statement (enum case)(忽略没有完整的switch语句) |
nls | to suppress warnings relative to non-nls string literals( 忽略非nls格式的字符) |
null | to suppress warnings relative to null analysis( 忽略对null的操作) |
rawtypes | to suppress warnings relative to un-specific types when using generics on class params( 使用generics时忽略没有指定相应的类型) |
restriction | to suppress warnings relative to usage of discouraged or forbidden references( 抑制禁止使用劝阻或禁止引用的警告) |
serial | to suppress warnings relative to missing serialVersionUID field for a serializable class( 忽略在serializable类中没有声明serialVersionUID变量) |
static-access | to suppress warnings relative to incorrect static access( 抑制不正确的静态访问方式警告) |
synthetic-access | to suppress warnings relative to unoptimized access from inner classes( 抑制子类没有按最优方法访问内部类的警告) |
unchecked | to suppress warnings relative to unchecked operations( 抑制没有进行类型检查操作的警告) |
unqualified-field-access | to suppress warnings relative to field access unqualified( 抑制没有权限访问的域的警告) |
unused | to suppress warnings relative to unused code( 抑制没被使用过的代码的警告) |
用来修饰注解的
表示注解作用范围,(源码中,class文件中,jvm中)
表示注解可以在哪里使用
表示javadoc生成文档时能看到该注解
表示子类可以继承该注解
tips:mac 中try-catch快捷键是option+command+t
错误Error
异常Exception:运行时异常、编译时异常 ,编译时异常必须要处理,运行时异常不处理会默认throws方式处理
在scanner接收到输入后,如果输入类型不对会报InputMismatchException异常
在把输入到的内容进行类型转换成数字时如果不对会报NumberFormatException异常
3. 子类抛出的异常不能比父类抛出异常的辈分更大
throws一般在方法的声明处,表示要抛出什么类型的异常,而throw在方法体中,抛出一个异常的对象