1.如何自定义枚举类。 枚举类:类的对象是有限个的,确定的。
1.1 私有化类的构造器,保证不能在类的外部创建其对象
1.2 在类的内部创建枚举类的实例。声明为:public static final
1.3 若类有属性,那么属性声明为:private final 。此属性在构造器中赋值。
2.使用enum关键字定义枚举类
>2.1其中常用的方法:values() valueOf(String name);
>2.2枚举类如何实现接口 :①让类实现此接口,类的对象共享同一套接口的抽象方法的实现。
①让类的每一个对象都去实现接口的抽象方法,进而通过类的对象调用被重写的抽象方法时,执行的效果不同
- public class TestSeason1 {
- public static void main(String[] args) {
- Season1 spring = Season1.SPRING;
- System.out.println(spring);
- spring.show();
- System.out.println(spring.getSeasonName());
-
- System.out.println();
- //1.values()
- Season1[] seasons = Season1.values();
- for(int i = 0;i < seasons.length;i++){
- System.out.println(seasons[i]);
- }
- //2.valueOf(String name):要求传入的形参name是枚举类对象的名字。
- //否则,报java.lang.IllegalArgumentException异常
- String str = "WINTER";
- Season1 sea = Season1.valueOf(str);
- System.out.println(sea);
- System.out.println();
-
- Thread.State[] states = Thread.State.values();
- for(int i = 0;i < states.length;i++){
- System.out.println(states[i]);
- }
- sea.show();
-
- }
- }
- interface Info{
- void show();
- }
- //枚举类
- enum Season1 implements Info{
- SPRING("spring", "春暖花开"){
- public void show(){
- System.out.println("春天在哪里?");
- }
- },
- SUMMER("summer", "夏日炎炎"){
- public void show(){
- System.out.println("生如夏花");
- }
- },
- AUTUMN("autumn", "秋高气爽"){
- public void show(){
- System.out.println("秋天是用来分手的季节");
- }
- },
- WINTER("winter", "白雪皑皑"){
- public void show(){
- System.out.println("冬天里的一把火");
- }
- };
-
- private final String seasonName;
- private final String seasonDesc;
-
- private Season1(String seasonName,String seasonDesc){
- this.seasonName = seasonName;
- this.seasonDesc = seasonDesc;
- }
- public String getSeasonName() {
- return seasonName;
- }
- public String getSeasonDesc() {
- return seasonDesc;
- }
-
- @Override
- public String toString() {
- return "Season [seasonName=" + seasonName + ", seasonDesc="
- + seasonDesc + "]";
- }
- // public void show(){
- // System.out.println("这是一个季节");
- // }
- }
复制代码 |
|