这篇技术博客是我复习尚硅谷JavaSE教程做的笔记总结,方便大家的学习同时也方便自己。博客内容非复制粘贴,纯手写。如果对你有帮助,欢迎点赞评论收藏!
枚举类的理解:类的对象只有有限个,确定的。我们称此类为枚举类
当需要定义一组常量时,强烈建议使用枚举类
如果枚举类中只有一个对象,则可以作为单例模式的实现方式
代码展示:
/**
* 枚举类的属性
* 1.枚举类对象的属性不应允许被改动, 所以应该使用 private final 修饰
* 2.枚举类的使用 private final 修饰的属性应该在构造器中为其赋值
* 3.若枚举类显式的定义了带参数的构造器, 则在列出枚举值时也必须对应的传入参数
*
* 注意点:
* 1. 私有化类的构造器,保证不能在类的外部创建其对象
* 2. 在类的内部创建枚举类的实例。声明为:public static final
* 3. 对象如果有实例变量,应该声明为private final,并在构造器中初始化
*/
public class SeasonTest {
public static void main(String[] args) {
Season spring = Season.SPRING;
System.out.println(spring);
}
}
//自定义枚举类
class Season{
//1.声明Season对象的属性:private final修饰
private final String seasonName;
private final String seasonDesc;
//2.私有化类的构造器
private Season(String seasonName,String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//3.提供当前枚举类的多个对象:public static final的
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("冬天","冰天雪地");
//4.其他诉求1:获取枚举类对象的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
//5.其他诉求2:提供toString()
@Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + '\'' +
", seasonDesc='" + seasonDesc + '\'' +
'}';
}
}
使用说明:
/**
* 使用enum关键字定义枚举类
* 说明:定义的枚举类默认继承于java.lang.Enum类,
* 所以toString方法不会打印地址值
*/
public class SeasonTest1 {
public static void main(String[] args) {
Season1 summer = Season1.SUMMER;
System.out.println(summer); //SUMMER
}
}
//使用enum关键字定义枚举类
enum Season1{
//1.提供当前枚举类的对象,多个对象之间有逗号隔开,末尾对象分号结束
SPRING("春天","春暖花开"),
SUMMER("夏天","夏日炎炎"),
AUTUMN("秋天","秋高气爽"),
WINTER("冬天","冰天雪地");
//2.声明Season对象的属性:private final修饰
private final String seasonName;
private final String seasonDesc;
//3.私有化类的构造器
private Season1(String seasonName,String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//4.其他诉求1:获取枚举类对象的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
}
我们今天讲解主要方法的使用,不全部讲述
1. values()方法:返回枚举类型的对象数组。该方法可以很方便地遍历所有的
枚举值。
2. valueOf(String str):可以把一个字符串转为对应的枚举类对象。要求字符
串必须是枚举类对象的“名字”。如不是,会有运行时异常:IllegalArgumentException。
3. toString():返回当前枚举类对象常量的名称
代码展示:
public class SeasonTest1 {
public static void main(String[] args) {
Season1 summer = Season1.SUMMER;
//toString()
System.out.println(summer.toString()); //SUMMER
//values()
Season1[] values = Season1.values();
for (int i = 0; i < values.length; i++) {
System.out.println(values[i]);
}
//valueOf(String objName):返回枚举类中对象名是objName的对象
Season1 winter = Season1.valueOf("WINTER");
System.out.println(winter); //WINTER
//Season1 winter = Season1.valueOf("WINTER1");
//如果objName的枚举类对象,则抛出异常:IllegalArgumentException
}
}
//使用enum关键字定义枚举类
enum Season1{
//1.提供当前枚举类的对象,多个对象之间有逗号隔开,末尾对象分号结束
SPRING("春天","春暖花开"),
SUMMER("夏天","夏日炎炎"),
AUTUMN("秋天","秋高气爽"),
WINTER("冬天","冰天雪地");
//2.声明Season对象的属性:private final修饰
private final String seasonName;
private final String seasonDesc;
//3.私有化类的构造器
private Season1(String seasonName,String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//4.其他诉求1:获取枚举类对象的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
}
代码展示:
/**
* 情况一:实现接口,在enum类中实现抽象方法
*/
public class SeasonTest1 {
public static void main(String[] args) {
Season1 summer = Season1.SUMMER;
summer.show();
}
}
//定义接口
interface Info{
void show();
}
//使用enum关键字定义枚举类
enum Season1 implements Info{
//1.提供当前枚举类的对象,多个对象之间有逗号隔开,末尾对象分号结束
SPRING("春天","春暖花开"),
SUMMER("夏天","夏日炎炎"),
AUTUMN("秋天","秋高气爽"),
WINTER("冬天","冰天雪地");
//2.声明Season对象的属性:private final修饰
private final String seasonName;
private final String seasonDesc;
//3.私有化类的构造器
private Season1(String seasonName,String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//4.其他诉求1:获取枚举类对象的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
//实现show()
@Override
public void show() {
System.out.println("这是一个季节");
}
}
代码展示:
/**
* 情况二:让枚举类的对象分别实现接口中的抽象方法
*/
public class SeasonTest1 {
public static void main(String[] args) {
Season1 summer = Season1.SUMMER;
Season1[] values = Season1.values();
for (int i = 0; i < values.length; i++) {
values[i].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("大约在冬季");
}
};
//2.声明Season对象的属性:private final修饰
private final String seasonName;
private final String seasonDesc;
//3.私有化类的构造器
private Season1(String seasonName,String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//4.其他诉求1:获取枚举类对象的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
}