Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
58 lines (54 loc) · 1.54 KB

File metadata and controls

58 lines (54 loc) · 1.54 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package enumt;
import java.util.HashMap;
import java.util.Map;
public enum Opreation {
PLUS("+"){ //枚举类型中的抽象方法必须被他所有的常量所实现
// 构造函数 使得其与数据关联,实现抽象方法使得其与行为关联
double apply()
{
return 0.1;
}
},
MINUX("-"){
double apply()
{
return 0.1;
}
},
TIMES("/"){
double apply()
{
return 0.1;
}
};
private final String symbol;
/*
* 使用静态方法,实现枚举与toString之间的映射
* 类加载的顺序:static变量、static 静态语句块 -- 其他成员变量 -- 构造函数
* */
private static Map<String,Opreation> list=new HashMap<>();
static {
for (Opreation o: Opreation.values()) {
list.put(o.toString(),o);
}
}
public static Opreation formString(String symbol){
return list.get(symbol);
}
Opreation(String symbol){
this.symbol=symbol;
}
abstract double apply();
@Override
public String toString() {
return symbol;
}
/*
* 每一个枚举都有一个ordinal(),表示常量在枚举列里边的索引值,从0 开始
* 但是 永远都不要用这个序数来表示你的枚举常量之间的差异和常量的特定含义
* 它是用来设计EnumSet 和EnumMap 这种基于枚举的通用数据结构
* */
public int getNumber(){
return ordinal()+1;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.