Java中的枚举
枚举概述
是指将变量的值 一 一 列出来,变量的值只限于列举出来的值的范围内。举例:一周只有7天,一年只有12个月等
回想单列设计模式:单例类是一个类只有一个实例
那么多例类就是一个类有多个实例,但不是无限个数的实例,而是有限个数的实例,这才能是枚举类
自己实现枚举
3版自定义枚举类(无参构造、带参构造、抽象方法(同时此类为抽象类))
案例1: 无参构造
案例2: 带参构造,get方法,toString方法
案例3: 抽象方法
参考代码:
package com.itheima_01;
/*
使用无参构造方法
*/
public class Light {
//创建三个实例、与单例设计模式不同
public static final Light RED = new Light();
public static final Light GREEN = new Light();
public static final Light YELLOW = new Light();
private Light(){} //防止外界创建对象
}
package com.itheima_01;
/*
使用带参构造方法
*/
public class Light2 {
//创建三个实例、与单例设计模式不同
public static final Light2 RED = new Light2("红");
public static final Light2 GREEN = new Light2("绿");
public static final Light2 YELLOW = new Light2("黄");
private String name;
// private Light2(){} //防止外界创建对象
private Light2(String name){
this.name = name;
}
public String getName(){
return name;
}
@Override
public String toString() {
return "Light2{" +
"name='" + name + '\'' +
'}';
}
}
package com.itheima_01;
/*
使用抽象类,来自定义枚举类
*/
public abstract class Light3 {
//创建三个实例、与单例设计模式不同
public static final Light3 RED = new Light3("红"){ //抽象类无法发直接实例化对象,使用匿名内部类的方式进行创建
@Override
public void show(){
System.out.println("红");
}
};
public static final Light3 GREEN = new Light3("绿"){
@Override
public void show(){
System.out.println("绿");
}
};
public static final Light3 YELLOW = new Light3("黄"){
@Override
public void show(){
System.out.println("黄");
}
};
private String name;
// private Light2(){} //防止外界创建对象
private Light3(String name){
this.name = name;
}
public String getName(){
return name;
}
@Override
public String toString() {
return "Light3{" +
"name='" + name + '\'' +
'}';
}
//添加了一个抽象方法,所以变成了抽象类
public abstract void show();
}
package com.itheima_01;
public class LightDemo {
public static void main(String[] args) {
Light r = Light.RED;
System.out.println(r); //com.itheima_01.Light@5594a1b5
System.out.println("----------");
Light2 r2 = Light2.RED;
System.out.println(r2); //Light2{name='红'}
System.out.println(r2.getName());
r2 = Light2.YELLOW;
System.out.println(r2);
System.out.println(r2.getName());
System.out.println("----------");
Light3 r3 = Light3.RED;
System.out.println(r3);
System.out.println(r3.getName());
r3.show();
}
}
从以上代码发现自己定义一个枚举类,比较麻烦,所以,Java就提供了枚举类供我们使用
格式是:只有枚举项的枚举类
public enum 枚举类名{
枚举项1,枚举项2,枚举项3......;
}
参考代码:
package com.itheima_02;
/*
使用无参构造
*/
public enum Light {
RED, GREEN, YELLOW;
}
/*
//原码解析:使用XJad反编译Class文件获得Light类的内容
public final class Light extends Enum{
public static final Light RED;
public static final Light GREEN;
public static final Light YELLOW;
static{
RED = new Light("RED", 0);
GREEN = new Light("GREEN", 1);
YELLOW = new Light("YELLOW", 2);
}
private Light(String s, int i)
{
super(s, i);
}
}
public abstract class Enum{
private final String name;
protected Enum(String name, int ordinal) {
this.name = name;
this.ordinal = ordinal;
}
public String toString() {
return name;
}
}
*/
package com.itheima_02;
/*
使用带参构造
*/
public enum Light2 {
// RED, GREEN, YELLOW; //相当于调用无参构造
RED("红"), GREEN("绿"), YELLOW("黄");
private String name;
private Light2(String name){
this.name = name;
}
public String getName(){
return name;
}
@Override
public String toString() {
return "Light2{" +
"name='" + name + '\'' +
'}';
}
}
package com.itheima_02;
/*
使用抽象方法
*/
public enum Light3 {
// RED, GREEN, YELLOW; //相当于调用无参构造
RED("红"){
@Override
public void show(){
System.out.println("红");
}
}, GREEN("绿"){
@Override
public void show(){
System.out.println("绿");
}
}, YELLOW("黄"){
@Override
public void show(){
System.out.println("黄");
}
};
private String name;
private Light3(String name){
this.name = name;
}
public String getName(){
return name;
}
@Override
public String toString() {
return "Light3{" +
"name='" + name + '\'' +
'}';
}
public abstract void show();
}
package com.itheima_02;
public class LightDemo {
public static void main(String[] args){
Light r = Light.RED;
System.out.println(r); //RED
System.out.println("----------");
Light2 r2 = Light2.RED;
System.out.println(r2);
System.out.println(r2.getName());
r2 = Light2.YELLOW;
System.out.println(r2);
System.out.println(r2.getName());
System.out.println("----------");
Light3 r3 = Light3.RED;
System.out.println(r3);
System.out.println(r3.getName());
r3.show();
}
}
枚举的注意事项
定义枚举类要用关键字enum
所有枚举类都是Enum的子类
枚举类的第一行上必须是枚举项,最后一个枚举项后面的分号可以省略的,但是如果枚举类有其他的东西,这个分号就不能省略。
枚举类可以有构造器,但是必须是private的,它默认的也是private的。枚举项的用法比较特殊:枚举("");
枚举类也可以有抽象方法,但是枚举项必须重写该方法
枚举在switch语句中使用
Light light = Light.RED;
switch(light){
case RED:
System.out.println("红");
break;
case GREEN:
System.out.println("绿");
break;
case YELLOW:
System.out.println("黄");
break;
//没有在枚举项中出现,无法识别
/*case BLUE:
System.out.println("蓝");
break;*/
}
标签:Java,name,36,System,枚举,println,public,out
From: https://www.cnblogs.com/fragmentary/p/17032192.html