Java 基础 enum枚举类 的创建/使用/接口继承 ,以及手动创建枚举类的对象为:public static final

笔记:

import java.lang.*;
/**一:枚举类 : enum Season implements info { s1(),s2(),s3(),s4() }; //s1--s4 放在Season类的最上面!
* 1.1.values() 返回一个包含整体的数组!
* 2.valueof(String name),返回对应name的类, 必须是枚举类对象的名字,否则报错!
* 3.实现接口,重写抽象方法 ,不同的枚举类可以有不同的方法
*/

枚举类代码:

interface info{
void show();
}
enum Season implements info{
Spring ("spring","春暖花开"){
@Override
public void show() {
//super.show();
System.out.println("春天在哪里!");
}
},
Summer("spring","夏日炎炎"){
public void show(){
System.out.println("夏荷才露尖尖角!");
}
},
Autumn("spring","秋高气爽"){
@Override
public void show() {
// super.show();
System.out.println("秋天像个*");
}
},
Winter("spring","白雪皑皑"){
@Override
public void show() {
super.show();
System.out.println("冬天来了,年来了!");
}
};
//1.提供类的属性,声明为private finall
private final String seasonName;
private final String seasonDesc;
//2.声明为final属性,在构造器中初始化
Season(String seasonName, String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//3.创建枚举类
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
//4.创建枚举类的对象为:public static final ,全局的常量 @Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + '\'' +
", seasonDesc='" + seasonDesc + '\'' +
'}';
} @Override
public void show() {
System.out.println("这是一个季节!");
}
}
public class Test_enum {
public static void main(String[] args) {
//1.values()
Season[] seasons= Season.values();
for(int i=0;i<seasons.length;i++)
System.out.println(seasons[i]);
//2.valueof(String name),必须是枚举类对象的名字,否则报错!
String str="Winter";
Season s1=Season.valueOf(str);
System.out.println("\n"+s1);
//2.1枚举Thread 的几个方法
System.out.println("\n"+"Thread枚举类的所有方法: ");
Thread.State[] states = Thread.State.values();
for(int i=0;i<states.length;i++)
System.out.print(states[i]+"\t");
System.out.println();
//3.实现接口,重写抽象方法 }
}

输出:

Season{seasonName='spring', seasonDesc='春暖花开'}
Season{seasonName='spring', seasonDesc='夏日炎炎'}
Season{seasonName='spring', seasonDesc='秋高气爽'}
Season{seasonName='spring', seasonDesc='白雪皑皑'} Season{seasonName='spring', seasonDesc='白雪皑皑'} Thread枚举类的所有方法:
NEW RUNNABLE BLOCKED WAITING TIMED_WAITING TERMINATED

手动创建等价的枚举类:

  //  手动创建'枚举类',
class Season{
//1.提供类的属性,声明为private finall
private final String seasonName;
private final String seasonDesc;
//2.声明为final属性,在构造器中初始化
public Season(String seasonName, String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//3.创建枚举类
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
//4.创建枚举类的对象为:public static final ,全局的常量
public static final Season Spring = new Season("spring","春暖花开");
public static final Season Summer = new Season("spring","夏日炎炎");
public static final Season Autumn = new Season("spring","秋高气爽");
public static final Season winter = new Season("spring","白雪皑皑");
@Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + '\'' +
", seasonDesc='" + seasonDesc + '\'' +
'}';
}
}
上一篇:学习子句管理策略总结1--On the learned clauses database management strategies


下一篇:What is nosql database?