枚举:jdk1.5后出的新特性。可以定义有限数量的可穷举数据集。--简而言之:当确定一个类有几个对象,使用枚举 1、自定义枚举类(jdk1.5前) 2、使用enum关键字创建枚举类 3、枚举类实现接口
1、自定义枚举类(jdk1.5前)
package JavaLearning;
/*
* 自定义枚举类
* */
public class Season {
private String seasonName;
private String seasonDesc;
//2、类的内部创建对象
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("冬天","冬天雪花飘");
//1、构造器私有化(不让外部随便创建对象)
private Season(String seasonName, String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
public String getSeasonName() {
return seasonName;
}
public void setSeasonName(String seasonName) {
this.seasonName = seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
public void setSeasonDesc(String seasonDesc) {
this.seasonDesc = seasonDesc;
}
@Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + '\'' +
", seasonDesc='" + seasonDesc + '\'' +
'}';
}
}
public class EnumTest {
public static void main(String[] args) {
Season spring=Season.SPRING;
System.out.println(spring);
}
}
2、使用enum关键字创建枚举类:
package JavaLearning;
/*
* 使用enum关键字创建枚举类
* */
public enum Season1 {
//2、类的内部创建对象(必须放在可执行代码的首航)
SPRING("春天","春眠不觉晓"),
SUMMER("夏天","夏天蚊子咬"),
AUTUMN("秋天","秋天叶子黄"),
WINTER("冬天","冬天雪花飘");
private String seasonName;
private String seasonDesc;
//1、构造器私有化(不让外部随便创建对象)
private Season1(String seasonName, String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
public String getSeasonName() {
return seasonName;
}
public void setSeasonName(String seasonName) {
this.seasonName = seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
public void setSeasonDesc(String seasonDesc) {
this.seasonDesc = seasonDesc;
}
@Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + '\'' +
", seasonDesc='" + seasonDesc + '\'' +
'}';
}
}
public class EnumTest {
public static void main(String[] args) {
Season1 spring=Season1.SPRING;
System.out.println(spring);
Season1 winter=Season1.WINTER;
System.out.println(winter);
}
}
3、
package JavaLearning;
public enum Season2 {
SPRING,
SUMMER,
AUTUMN,
WINTER;
}
public class EnumTest {
public static void main(String[] args) {
Season2 spring=Season2.SPRING;
System.out.println(spring);
Season2 winter=Season2.WINTER;
System.out.println(winter);
}
}
直接将对象的引用打印输出
public class EnumTest {
public static void main(String[] args) {
Season2 spring=Season2.SPRING;
System.out.println(spring);
switch(spring){
case SPRING:
System.out.println("春天");
break;
case SUMMER:
System.out.println("夏天");
break;
case AUTUMN:
System.out.println("秋天");
break;
case WINTER:
System.out.println("冬天");
break;
}
}
}
package JavaLearning;
/*
枚举:jdk1.5后出的新特性。可以定义有限数量的可穷举数据集。--简而言之:当确定一个类有几个对象,使用枚举
1、自定义枚举类(jdk1.5前)
①构造器私有化
②类的内部创建对象
2、使用enum关键字创建枚举类
----> valueOf(String name) :根据枚举类对象的名称,获取对应的枚举类对象
----> values() :获取当前枚举类中所有的枚举类对象组成的数组
3、枚举类实现接口
*/
public class EnumTest {
public static void main(String[] args) {
// Season spring=Season.SPRING;
// System.out.println(spring);
// Season winter=Season.WINTER;
// System.out.println(winter);
// Season1 spring=Season1.SPRING;
// System.out.println(spring);
// Season1 winter=Season1.WINTER;
// System.out.println(winter);
Season2 spring=Season2.SPRING;
System.out.println(spring);
Season2 winter=Season2.WINTER;
System.out.println(winter);
// switch(spring){
// case SPRING:
// System.out.println("春天");
// break;
// case SUMMER:
// System.out.println("夏天");
// break;
// case AUTUMN:
// System.out.println("秋天");
// break;
// case WINTER:
// System.out.println("冬天");
// break;
// }
// Season2 season=Season2.valueOf("SUMMER");
// System.out.println(season);
Season2[] seasons=Season2.values();
for (Season2 season:seasons){
System.out.println(season);
}
}
}
枚举类实现接口:
package JavaLearning;
public enum Season2 implements MyInterface2{
SPRING,
SUMMER,
AUTUMN,
WINTER;
public void show(){
System.out.println("季节");
}
}
package JavaLearning;
public interface MyInterface2 {
public abstract void show();
}
优化:每个枚举类都去单独实现show方法
package JavaLearning;
public enum Season2 implements MyInterface2{
SPRING{
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("冬天");
}
};
// public void show(){
// System.out.println("季节");
// }
}
内部类:
练习:
* 1.声明Week枚举类,其中包含星期一至星期日的定义; * 2.在TestWeek类中声明方法中printWeek(Week week),根 据参数值打印相应的中文星期字符串。 提示,使用switch语句实现。 * 3.在main方法中从命令行接收一个1-7的整数(使用 Integer.parseInt方法转换),分别代表星期一至星期日,打印该值对应的枚举值,然后以此枚举值调用printWeek方法输出中文星期。
package JavaLearning;
public enum Week {
MONDAY,
TUESDAY,
WENDSDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY;
}
package JavaLearning;
/*
*
* 1.声明Week枚举类,其中包含星期一至星期日的定义;
* 2.在TestWeek类中声明方法中printWeek(Week week),根
据参数值打印相应的中文星期字符串。
提示,使用switch语句实现。
* 3.在main方法中从命令行接收一个1-7的整数(使用
Integer.parseInt方法转换),分别代表星期一至星期日,打印该值对应的枚举值,然后以此枚举值调用printWeek方法输出中文星期。
*
* */
public class WeekTest {
public static void main(String[] args) {
int num=Integer.parseInt(args[0]);
Week[] weeks= Week.values();
printWeek(weeks[num-1]);
// switch (num){
// case 1:
// printWeek(Week.MONDAY);
// break;
// case 2:
// printWeek(Week.TUESDAY);
// break;
// case 3:
// printWeek(Week.WENDSDAY);
// break;
// case 4:
// printWeek(Week.THURSDAY);
// break;
// case 5:
// printWeek(Week.FRIDAY);
// break;
// case 6:
// printWeek(Week.SATURDAY);
// break;
// case 7:
// printWeek(Week.SUNDAY);
// break;
// }
}
public static void printWeek(Week week){
switch (week){
case MONDAY:
System.out.println("星期一");
break;
case TUESDAY:
System.out.println("星期二");
break;
case WENDSDAY:
System.out.println("星期三");
break;
case THURSDAY:
System.out.println("星四");
break;
case FRIDAY:
System.out.println("星期五");
break;
case SATURDAY:
System.out.println("星期六");
break;
case SUNDAY:
System.out.println("星期日");
break;
}
}
}
命令行传值: