枚举的定义和使用
- 枚举不能继承
- 枚举不能被继承
定义枚举
enum enumName
代码示例
public enum Language {
Java,Php,C,Golang,Dart,Flutter
}
代码测试使用
public static void main(String[] args) {
Language lang;
lang = Language.Java;
switch (lang){
case C:
System.out.println("C");
break;
case Golang:
System.out.println("go");
break;
default:
System.out.println("other");
}
Language[] ll = Language.values();
System.out.println(ll.length);
for(Language l : ll){
System.out.println(l);
}
}
枚举的其他作用
- 枚举是类类型
- 枚举不能使用new 关键字
- 提供构造函数
- 添加实例变量
- 添加方法
- 实现接口
枚举构造器
先看代码示例
public enum Language {
Java(1),Php,C(2),Golang(3),Dart(5),Flutter;
int sort = 1;
Language(){
this.sort = 100;
}
Language(int sort){
this.sort = sort;
}
}
代码测试
Language[] ls = Language.values();
for(Language l : ls){
System.out.println(l + ":\t"+ l.sort);
}
输出
Java: 1
Php: 100
C: 2
Golang: 3
Dart: 5
Flutter: 100
从以上代码和结果可以看出,要想实现 ,Dart(5),Flutter;这种方式需要有两个构造器,一个有参,一个没有参。没有参的构造器会默认使用默认值
构造器扩展
代码
public enum Language {
Java(1),Php,C(2),Golang("go语言"),Dart(5),Flutter,C1("C++",2),Objectc(MainTest.mm);
int sort ;
String name;
Map<String,Object> m;
Language(){
this.sort = 100;
}
Language(int sort){
this.sort = sort;
}
Language(String name){
this.name = name;
}
Language(String name,int sort){
this.name = name;
this.sort = sort;
}
Language(Map<String,Object> m){
this.m = m;
}
}
测试类
public class MainTest {
static Map<String,Object> mm = new HashMap<>();
static{
mm.put("m1",12334);
mm.put("m2",new int[]{1,2,34,});
mm.put("m3",true);
}
测试代码
Language[] ls = Language.values();
for(Language l : ls){
System.out.println(l + ":\t"+ l.sort + "\t" + l.name + "\t" + l.m);
}
测试输出
Java: 1 null null
Php: 100 null null
C: 2 null null
Golang: 0 go语言 null
Dart: 5 null null
Flutter: 100 null null
C1: 2 C++ null
Objectc: 0 null {m1=12334, m2=[I@1e127982, m3=true}
构造函数中没有的参数会默认null,根据定义的类型会设置不同的数据
枚举自定义方法
与类形同
举例:
public void test(){
System.out.println("language");
}
测试
Language java = Language.Java;
java.test();
枚举中的方法
枚举默认继承Enum类
final int ordinal() 检索序数值
Language[] ls = Language.values();
for(Language l : ls){
System.out.println(l + "\t" + l.ordinal());
}
输出结果
Java 0
Php 1
C 2
Golang 3
Dart 4
Flutter 5
C1 6
Objectc 7
final int compareTo(enum-type e) 测试代码
Language java = Language.Java;
Language c = Language.C;
Language f = Language.Flutter;
System.out.println(java.compareTo(c));
System.out.println(f.compareTo(c));
输出结果
-2
3
从以上结果可以看出,如果在前返回为负数,在后返回为正数,位置为,从当前数据为0开始的第n个元素就是目标元素
equals 比较是否相等
Enum 的方法 public final boolean equals(Object other) { return this==other; }
可以看出其中还是使用的 “==”,而且不允许被重写
枚举equals的比较
Language java = Language.Java;
Language java1 = Language.Java;
Language f = Language.Flutter;
System.out.println(java.equals("Java"));
System.out.println(java1.equals(java));
System.out.println(f.equals(java));
System.out.println(java == java1 );
System.out.println(java == f );
输出结果
false
true
false
true
false
最后的联系,快速答题器
public class QuickAnswer {
public static void main(String[] args) {
answer(ask());
answer(ask());
answer(ask());
answer(ask());
answer(ask());
answer(ask());
answer(ask());
answer(ask());
}
public static Answer ask(){
Random r = new Random();
int num = (int)(100 * r.nextDouble());
if(num <15)
return Answer.MAYBE;
if(num <30)
return Answer.NO;
if(num < 60)
return Answer.Yes;
if(num < 75)
return Answer.LATER;
if(num<98)
return Answer.SOON;
return Answer.NEVER;
}
public static void answer(Answer answer){
switch (answer){
case NEVER:
System.out.println("naver");
break;
case SOON:
System.out.println("soon");
break;
case LATER:
System.out.println("later");
break;
case Yes:
System.out.println("yes");
break;
case NO:
System.out.println("no");
break;
case MAYBE:
System.out.println("maber");
break;
default:
System.out.println("default");
}
}
}
enum Answer{
NO,Yes,MAYBE,LATER,SOON,NEVER
}
|