对枚举类型的学习
来源:互联网 发布:资金管理股票知乎 编辑:程序博客网 时间:2024/04/29 03:23
通过实例学习枚举类型:
实例一、
public enum FirstWeather {
Sunny("晴天"), Rainy("雨天"), Cloudy("多云");
private String value;// 私有成员变量,保存名称
public String getValue() {
return value;
}
FirstWeather(String value) {// 带参构造函数
this.value = value;
}
public static void main(String[] args) {
System.out.println(FirstWeather.Sunny.getValue());// 以下输出为"晴天"
FirstWeather w=FirstWeather.Cloudy;
System.out.println(w.getValue());// 以下输出为"多云"
}
}
实例二、
public enum SecondWeather {
Sunny(1,"晴天"), Rainy(2,"雨天"), Cloudy(3,"多云");
private int value;
private String label;
private SecondWeather(int value, String label) {
this.value = value;
this.label = label;
}
public int getValue() {
return value;
}
public String getLabel() {
return label;
}
public static SecondWeather parse(int value) {
SecondWeather result = SecondWeather.Sunny;
switch (value) {
case 1:
result = SecondWeather.Sunny;
break;
case 2:
result = SecondWeather.Rainy;
break;
case 3:
result = SecondWeather.Cloudy;
break;
}
return result;
}
public static List<SecondWeather> getEnumValues() {
return Arrays.asList(values());
}
public static void main(String[] args) {
System.out.println(SecondWeather.Sunny.getValue()+":"+SecondWeather.Sunny.getLabel());// 以下输出为"1:晴天"
SecondWeather w=SecondWeather.Cloudy;
System.out.println(w.getValue()+":"+w.getLabel());// 以下输出为"3:多云"
List<SecondWeather> list=getEnumValues();
for (SecondWeather sw:list) {
System.out.println(sw.value+"--"+sw.label);//依次输出:1--晴天、2--雨天、3--多云
}
}
}
实例三、
import java.util.Arrays;
import java.util.List;
public enum Weather {
Sunny(1,"晴天") {
@Override
public String test() {
return "今天阳光明媚!!!\n";
}
},
Rainy(2,"雨天") {
@Override
public String test() {
return "出门别忘带把雨伞哦!!!\n";
}
},
Cloudy(3,"多云") {
@Override
public String test() {
return "别出去登高望远了,你看不远的,呵呵呵!!!\n";
}
};
private int value;
private String label;
private Weather(int value, String label) {
this.value = value;
this.label = label;
}
public int getValue() {
return value;
}
public String getLabel() {
return label;
}
public abstract String test();
public static Weather parse(int value) {
Weather result = Weather.Sunny;
switch (value) {
case 1:
result = Weather.Sunny;
break;
case 2:
result = Weather.Rainy;
break;
case 3:
result = Weather.Cloudy;
break;
}
return result;
}
public static List<Weather> getEnumValues() {
return Arrays.asList(values());
}
public static void main(String[] args) {
System.out.println(Weather.Sunny.getValue()+":"+Weather.Sunny.getLabel());
Weather weather=Weather.Cloudy;
System.out.println(weather.getValue()+":"+weather.getLabel()+"\n\n");
List<Weather> list=getEnumValues();
for (Weather sw:list) {
System.out.println(sw.value+"--"+sw.label+"--"+sw.test());
}
}
}
- 对枚举类型的学习
- 对Java中的枚举类型的学习总结
- 对Java中的枚举类型的学习总结
- 对枚举类型的处理
- 自定义对枚举类型的读写函数
- jackson对枚举类型的序列化
- 关于枚举类型的学习(enum)
- enum枚举类型学习
- 枚举类型学习
- 枚举类型学习
- [C#学习]枚举类型
- java枚举类型学习
- Java 枚举类型学习
- 学习枚举类型
- 透过对类的加载看JAVA中的枚举类型
- 对C++枚举类型的进一步思考以及简化
- C++对C的扩展之枚举类型enum
- 对Java中枚举类型Enum的理解
- position:absolute与overflow:hidden
- HDU 1814 2-sat 求字典序最小解
- log4j additivity解说
- 第十一周 输出星号图2
- Uva1398 Meteor
- 对枚举类型的学习
- 调用函数printchs输出星号图
- 求解求最大公约数定义的算法,这个程序求不出来,网上百度的算法不对?
- javascript 性能分析:dom 编程
- PHP的验证码
- Runtime中神奇的exec方法
- jQuery中append、insertBefore、after与insertAfter方法注意事项
- Lucene4.3开发之第一步小试牛刀
- 简单星号图