java中枚举都继承自java.lang.Enum类,所以枚举类不能继承别的类,但是可以实现接口。这里我们定义一个WeekInterface接口:
public interface WeekInterface {
public String getWeekInfo();
}
下面是WeekEnum枚举类并实现WeekInterface接口:
public enum WeekEnum implements WeekInterface{
Mon("星期一","Monday",1),
Tue("星期二","Tuesday",2),
Wed("星期三","Wednesday",3),
Thu("星期四","Thursday",4),
Fri("星期五","Friday",5),
Sat("星期六","Saturday",6),
Sun("星期日","Sunday",7);
private String nameCN;
private String nameEN;
private int value;
private WeekEnum(String nameCN,String nameEN,int value){
this.nameCN=nameCN;
this.nameEN=nameEN;
this.value=value;
}
public String getNameCN() {
return nameCN;
}
public void setNameCN(String nameCN) {
this.nameCN = nameCN;
}
public String getNameEN() {
return nameEN;
}
public void setNameEN(String nameEN) {
this.nameEN = nameEN;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
@Override
public String getWeekInfo() {
return this.nameCN+this.nameEN+this.value;
}
}
public class WeekClass implements WeekInterface{
private String nameCN;
private String nameEN;
private int value;
public WeekClass(String nameCN,String nameEN,int value) {
this.nameCN=nameCN;
this.nameEN=nameEN;
this.value=value;
}
public String getNameCN() {
return nameCN;
}
public void setNameCN(String nameCN) {
this.nameCN = nameCN;
}
public String getNameEN() {
return nameEN;
}
public void setNameEN(String nameEN) {
this.nameEN = nameEN;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
@Override
public String getWeekInfo() {
return this.nameCN+this.nameEN+this.value;
}
}
发现没有,枚举和类之间的区别?
其实一个枚举类型就相当于java类有限的实体类集合。