image.png image.png

关键字

image.png

  1. public class Demo02 {
  2. public static void main(String[] args) {
  3. System.out.println(Season.SPRING);
  4. }
  5. }
  6. enum Season {
  7. SPRING("春天"),SUMMER("夏天");
  8. private String name;
  9. private Season(String name) {
  10. this.name = name;
  11. }
  12. public String getName() {
  13. return name;
  14. }
  15. @Override
  16. public String toString() {
  17. return "Season{" +
  18. "name='" + name + '\'' +
  19. '}';
  20. }
  21. }

image.png
image.png

  1. import javax.swing.*;
  2. public class Demo02 {
  3. public static void main(String[] args) {
  4. System.out.println(Season.SPRING);
  5. Season [] seasons = Season.values();
  6. for (Season season :seasons) {
  7. System.out.println(season);
  8. }
  9. System.out.println(Season.SPRING.name());
  10. System.out.println(Season.SPRING.ordinal());
  11. System.out.println(Season.SPRING.compareTo(Season.SUMMER));
  12. }
  13. }
  14. enum Season {
  15. SPRING("春天"),SUMMER("夏天");
  16. private String name;
  17. private Season(String name) {
  18. this.name = name;
  19. }
  20. public String getName() {
  21. return name;
  22. }
  23. @Override
  24. public String toString() {
  25. return "Season{" +
  26. "name='" + name + '\'' +
  27. '}';
  28. }

image.png

  1. public class Demo03 {
  2. public static void main(String[] args) {
  3. System.out.println("星期信息如下:");
  4. Week [] weeks = Week.values();
  5. for (Week week:weeks){
  6. System.out.println(week);
  7. }
  8. }
  9. }
  10. enum Week{
  11. MONDAY("星期一"),TUESDAY("星期二"),WEDNESDAY("星期三"),
  12. THURSDAY("星期四"),FRIDAY("星期五"),SATURDAY("星期六"),SUNDAY("星期天");
  13. private String name;
  14. private Week(String name) {
  15. this.name = name;
  16. }
  17. public String getName() {
  18. return name;
  19. }
  20. @Override
  21. public String toString() {
  22. return name;
  23. }
  24. }
  • enum 关键字修饰的类 底层是一个final修饰的 继承 enum的类,所以不能再继承其他类,可以实现接口欧