标签:des war val 存在 summer dep 构造 调用 value
1 public class TestSeason { 2 3 public static void main(String[] args) { 4 Season spring = Season.Spring; 5 System.out.println(spring); 6 } 7 } 8 public class Season { 9 //将属性定义为私有常量 10 private final String seasonName; 11 private final String seasonDes; 12 13 //将构造器设置为私有的 14 private Season (String seasonName, String seasonDes) { 15 this.seasonName = seasonName; 16 this.seasonDes = seasonDes; 17 } 18 //利用构造器构造春夏秋冬四个对象 19 public static final Season Spring = new Season("春天","万物复苏"); 20 public static final Season Summer = new Season("夏天","骄阳似火"); 21 public static final Season Fall = new Season("秋天","秋高气爽"); 22 public static final Season Winter = new Season("冬天","白雪皑皑"); 23 //因为属性是常量,所以不能赋值 24 public String getSeasonName() { 25 return seasonName; 26 } 27 public String getSeasonDes() { 28 return seasonDes; 29 } 30 //重写toString方法 31 @Override 32 public String toString() { 33 return "Season [seasonName=" + seasonName + ", seasonDes=" + seasonDes + "]"; 34 } 35 36 }
1 public class TestSeason { 2 3 public static void main(String[] args) { 4 Season1 spring = Season1.Spring; 5 System.out.println(spring); 6 //使用values()方法将所有对象存入一个数组中 7 Season1[] season = Season1.values(); 8 for (int i = 0; i < season.length; i++) { 9 System.out.println(season[i]); 10 } 11 //使用valueOf(String name)方法将指定对象名的对象取出来 12 String name = "Winter"; 13 Season1 winter = Season1.valueOf(name); 14 System.out.println(winter); 15 } 16 } 17 public enum Season1 { 18 Spring("春天","万物复苏"), 19 Summer("夏天","骄阳似火"), 20 Fall("秋天","秋高气爽"), 21 Winter("冬天","白雪皑皑"); 22 23 private final String seasonName; 24 private final String seasonDes; 25 26 private Season1 (String seasonName, String seasonDes) { 27 this.seasonName = seasonName; 28 this.seasonDes = seasonDes; 29 } 30 31 public String getSeasonName() { 32 return seasonName; 33 } 34 public String getSeasonDes() { 35 return seasonDes; 36 } 37 38 @Override 39 public String toString() { 40 return "Season1 [seasonName=" + seasonName + ", seasonDes=" + seasonDes + "]"; 41 } 42 43 }
1 public class TestSeason { 2 3 public static void main(String[] args) { 4 Season1 spring = Season1.Spring; 5 System.out.println(spring); 6 //使用values()方法将所有对象存入一个数组中 7 Season1[] season = Season1.values(); 8 for (int i = 0; i < season.length; i++) { 9 System.out.println(season[i]); 10 } 11 //使用valueOf(String name)方法将指定对象名的对象取出来 12 String name = "Winter"; 13 Season1 winter = Season1.valueOf(name); 14 System.out.println(winter); 15 //调用接口中的方法 16 spring.Show(); 17 } 18 } 19 public interface Show { 20 void Show(); 21 } 22 public enum Season1 implements Show { 23 Spring("春天","万物复苏"){ 24 @Override 25 public void Show() { 26 System.out.println("春天:"+Spring.toString()); 27 } 28 }, 29 Summer("夏天","骄阳似火"){ 30 @Override 31 public void Show() { 32 System.out.println("夏天:"+Spring.toString()); 33 } 34 }, 35 Fall("秋天","秋高气爽"){ 36 @Override 37 public void Show() { 38 System.out.println("秋天:"+Spring.toString()); 39 } 40 }, 41 Winter("冬天","白雪皑皑"){ 42 @Override 43 public void Show() { 44 System.out.println("冬天:"+Spring.toString()); 45 } 46 }; 47 48 private final String seasonName; 49 private final String seasonDes; 50 51 private Season1 (String seasonName, String seasonDes) { 52 this.seasonName = seasonName; 53 this.seasonDes = seasonDes; 54 } 55 56 public String getSeasonName() { 57 return seasonName; 58 } 59 public String getSeasonDes() { 60 return seasonDes; 61 } 62 63 @Override 64 public String toString() { 65 return "Season1 [seasonName=" + seasonName + ", seasonDes=" + seasonDes + "]"; 66 } 67 }
标签:des war val 存在 summer dep 构造 调用 value
原文地址:https://www.cnblogs.com/jbrr/p/10305754.html