424,枚举类引出
package com.hspedu.enum_; public class Enumeration { public static void main(String[] args) { //使用 Season spring = new Season("春天","温暖"); Season summer = new Season("夏天","炎热"); Season autumn = new Season("秋天","凉爽"); Season winter = new Season("冬天","寒冷"); //这样也不合适,要固定才行 winter.setName("xxx"); winter.setDesc("非常的热"); //因为对于季节而言,它的对象(具体值),是固定的四个,不会有更多 //按老师的这个设计类的思路,不能体现季节是固定的四个对象 //因此,这样设计不好---> 引出枚举类[枚:一个一个 举:例举,即把具体的对象一个一个例举出来的类] Season other = new Season("红天","~~~"); //这个对象间接表示季节不是固定的四个 } } class Season{ private String name; private String desc; public Season(String name, String desc) { this.name = name; this.desc = desc; //描述 } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
425,自定义枚举类
package com.hspedu.enum_; import java.sql.SQLOutput; public class Enumeration { public static void main(String[] args) { System.out.println(Season.SPRING); } } //演示自定义枚举实现 class Season{ private String name; private String desc; //定义了四个对象吗,固定 public final static Season SPRING = new Season("春天","温暖"); public final static Season SUMMER = new Season("夏天","炎热"); public final static Season AUTUMN = new Season("秋天","凉爽"); public final static Season WINTER = new Season("冬天","寒冷"); //1,将构造器私有化,目的防止 直接 new //2,去掉setXxx方法,防止属性被修改 //3,在Season内部,直接创建固定的对象 //4,优化,可以在定义静态对象那里加入 final 修饰符 private Season(String name, String desc) { this.name = name; this.desc = desc; //描述 } public String getDesc() { return desc; } public String getName() { return name; } @Override public String toString() { return "Season{" + "name='" + name + '\'' + ", desc='" + desc + '\'' + '}'; } }
426,enum枚举类1
package com.hspedu.enum_; import java.sql.SQLOutput; public class Enumeration { public static void main(String[] args) { System.out.println(Season.SPRING); } } //演示使用enum关键字来实现枚举类 enum Season{ // //定义了四个对象吗,固定 // public final static Season SPRING = new Season("春天","温暖"); // public final static Season SUMMER = new Season("夏天","炎热"); // public final static Season AUTUMN = new Season("秋天","凉爽"); // public final static Season WINTER = new Season("冬天","寒冷"); //如果使用了enum来实现枚举类 //1,使用关键字 enum 替代 class //2,public final static Season SPRING = new Season("春天","温暖") 直接使用下面的代码 // SPRING("春天","温暖") 解读 常量名(实参列表) //3,如果有多个常量(对象),使用 , 号间隔即可 //4,如果使用enum 来实现枚举,要求将定义常量对象,写在前面 SPRING("春天","温暖"),SUMMER("夏天","炎热"),AUTUMN("秋天","凉爽"),WINTER("冬天","寒冷"); private String name; private String desc; private Season(String name, String desc) { this.name = name; this.desc = desc; //描述 } public String getDesc() { return desc; } public String getName() { return name; } @Override public String toString() { return "Season{" + "name='" + name + '\'' + ", desc='" + desc + '\'' + '}'; } }
427,enum枚举类2
第1点:
第3点
package com.hspedu.enum_; import java.sql.SQLOutput; public class Enumeration { public static void main(String[] args) { System.out.println(Season.What); } } //演示使用enum关键字来实现枚举类 enum Season{ // //定义了四个对象吗,固定 // public final static Season SPRING = new Season("春天","温暖"); // public final static Season SUMMER = new Season("夏天","炎热"); // public final static Season AUTUMN = new Season("秋天","凉爽"); // public final static Season WINTER = new Season("冬天","寒冷"); //如果使用了enum来实现枚举类 //1,使用关键字 enum 替代 class //2,public final static Season SPRING = new Season("春天","温暖") 直接使用下面的代码 // SPRING("春天","温暖") 解读 常量名(实参列表) //3,如果有多个常量(对象),使用 , 号间隔即可 //4,如果使用enum 来实现枚举,要求将定义常量对象,写在前面 //5,如果我们使用的是无参构造器,创建常量对象,则可以省略 (),例如 What 或 What() SPRING("春天","温暖"),SUMMER("夏天","炎热"), AUTUMN("秋天","凉爽"),WINTER("冬天","寒冷"), What(); private String name; private String desc; private Season() {//无参构造器 } private Season(String name, String desc) { this.name = name; this.desc = desc; //描述 } public String getDesc() { return desc; } public String getName() { return name; } @Override public String toString() { return "Season{" + "name='" + name + '\'' + ", desc='" + desc + '\'' + '}'; } }
428,enum枚举类3
package com.hspedu.enum_; import java.sql.SQLOutput; public class Enumeration { public static void main(String[] args) { Gender2 boy = Gender2.BOY; //ok Gender2 boy2 = Gender2.BOY; //ok System.out.println(boy); //输出BOY //本质就是调用Gender2 的父类 Enum的 toSpring方法 // public String toString() { // return name; // } System.out.println(boy2 == boy); //True,返回的是同一个对象名 } } enum Gender2{ //会调用父类 Enum 的 toString,返回枚举对象名 BOY,GIRL; }
429,Enum成员方法
位置号也是编号
package com.hspedu.enum_; import java.lang.invoke.VarHandle; import java.sql.SQLOutput; public class EnumMethod { public static void main(String[] args) { //使用Season 枚举类,来演示各种方法 Season autumn = Season.AUTUMN; //输出枚举对象的名字 System.out.println(autumn.name()); //ordinal() 输出的是该枚举对象的次序/编号,从0开始编号 //AUTUMN 枚举对象是第三个,因此输出 2 System.out.println(autumn.ordinal()); //从反编译(javap)可以看出 values方法,返回 Season[] //含有定义的所有枚举对象 Season[] values = Season.values(); System.out.println("====遍历取出枚举对象(增强for)===="); for(Season season:values){ //增强for循环 System.out.println(season); } //valueOf:将字符串转换成枚举对象,要求字符串必须为已有的常量名,否则报异常 //执行流程 //1,根据你输入的 "AUTUMN" 到 Season的枚举对象去查找 //2,如果找到了,就返回,如果没有找到,就报错 Season autumn1 = Season.valueOf("AUTUMN"); System.out.println("autumn1 = " + autumn1); System.out.println(autumn == autumn1); //compareTo:比较两个枚举常量,比较的就是编号 //老韩解读 //1,就是把 Season.AUTUMN 枚举对象的编号 和 Season.SUMMER枚举对象的编号比较 //2,看看结果 /* public final int compareTo(E o) { // 省略了一些代码,自己想看可以点进去看 return self.ordinal - other.ordinal; } Season.AUTUMN的编号 - Season.SUMMER的编号 = 2-1=1 */ System.out.println(Season.AUTUMN.compareTo(Season.SUMMER)); // //补充了一个增强for循环 // int[] nums = {1,2,9}; // //普通的for循环 // System.out.println("=====普通的for====="); // for(int i = 0; i < nums.length; i++) // { // System.out.println(nums[i]); // } // System.out.println("======增强的for======"); // //执行的流程是 依次从nums数组中取出数据,赋给i,如果取出完毕,则退出for // for(int i : nums) // { // System.out.println("i = " + i); // } } }
430,Enum课堂练习
package com.hspedu.enum_; public class EnumExercise { public static void main(String[] args) { //获取到所有的枚举对象,即数组 Week[] weeks = Week.values(); //遍历,使用增强for循环 System.out.println("=====所有星期的信息如下====="); for (Week week : weeks) { System.out.println(week); } } } enum Week{ //定义Week的枚举对象 MONDAY("星期一"),TUESDAY("星期二"), WEDNESDAY("星期三"), THURSDAY("星期四"),FRIDAY("星期五"),SATURDAY("星期六"),SUNDAY("星期日"); private String name; private Week(String name) { //构造器 this.name = name; } @Override public String toString() { //重写toSring()方法 return name; } }
431,Enum使用细节
package com.hspedu.enum_; public class EnumDetail { public static void main(String[] args) { Music.CLASSICMUSIC.playing(); } } class A { } //1,使用enum关键字后,就不能再继承其他类了,因为enum会隐式继承Enum,而Java是单继承机制 //enum Season extend A { //} //2,enum实现的枚举类,仍然是一个类,所以还是可以实现接口的 interface IPlaying{ public void playing(); } enum Music implements IPlaying{ CLASSICMUSIC; @Override public void playing() { System.out.println("播放好听的音乐..."); } }
注解可以看老韩的笔记。
441,家庭作业6
package com.hspedu.enum_; public class Homework { public static void main(String[] args) { //演示一下枚举值的switch使用 Color green = Color.GREEN; green.show(); //比较一下 //switch() 中,放入枚举对象 //在每个case后,直接写上在枚举类中,定义的枚举对象即可 switch (green) { case YELLOW: System.out.println("匹配到黄色"); case BLACK: System.out.println("匹配到黑色"); break; case GREEN: System.out.println("匹配到绿色"); break; default: System.out.println("没有匹配到..."); } } } interface IMyInterface { public void show(); } enum Color implements IMyInterface{ //定义枚举对象 RED(255,0,0),BLUE(0,0,255), BLACK(0,0,0),YELLOW(255,255,0), GREEN(0,255,0); private int redValue; private int greenValue; private int blueValue; Color(int redValue, int greenValue, int blueValue) { this.redValue = redValue; this.greenValue = greenValue; this.blueValue = blueValue; } @Override public void show() { System.out.println("属性值为" + redValue + "," + greenValue + "," + blueValue); } }
标签:java,String,Season,enum,name,枚举,public,顺平 From: https://www.cnblogs.com/romantichuaner/p/18182775