1.定义:
2.枚举类和普通类的区别:
- 枚举类的默认修饰符是 public static final
- 每一个枚举值是
- 枚举值是枚举值所属的类的数据类型,枚举值是指向本类的对象
- 枚举类的默认构造是private,可以自定义,但必须private
- 可以定义抽象方法,但是必须用枚举值实现
- 枚举值必须在自定义成员变量之前
使用要点:
- 在switch..case..中使用,case后面只需要枚举值不需要类.枚举值的形式
2.1枚举类的简单构建:
package com.company.project.enumTest;
//SeasonEnum.java
public enum SeasonEnum {
SPRING, SUMMER, FALL, WINTER;
}
package com.company.project.enumTest;
//EnumPrint.java
public class EnumPrint {
public void judge(SeasonEnum s){
switch(s){
case SPRING:
{
System.out.println("Season is SPRING");
break;
}
case SUMMER:
{
System.out.println("Season is SUMMER");
break;
}
case FALL:
{
System.out.println("Season is FALL");
break;
}
case WINTER:
{
System.out.println("Season is WINTER");
break;
}
}
}
public static void main(String[] args){
//Enumeration class has the default values method, which returns all instances of the enumeration class
for(SeasonEnum s : SeasonEnum.values()){
System.out.println("Enum.values :" + s);
}
//
new EnumPrint().judge(SeasonEnum.FALL);
}
}
2.2枚举类的成员变量、方法和构造
枚举类的成员变量:
//Gender.java
public enum Gender {
MALE,FEMALE;
public String name;
}
//GenderTest.java
public class GenderTest {
public static void main(String[] args) {
//通过valueOf获取枚举类的枚举值
Gender g = Enum.valueOf(Gender.class, "MALE");
g.name = "男";
System.out.println(g + "的枚举值是" + g.name);
}
}
上述代码修改完善:
//Gender.java
public enum Gender {
MALE,FEMALE;
//definde a var
//public String name;
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
switch(this){
case MALE:
{
if (name.equals("男")) {
this.name = name;
}
else{
System.out.println("Error!");
}
break;
}//case MALE
case FEMALE:{
if (name.equals("女")) {
this.name = name;
} else {
System.out.println("Error!");
}
break;
}//case FEMALE
}
}
}
//GenderTest.java
public class GenderTest {
public static void main(String[] args) {
//通过valueOf获取枚举类的枚举值
Gender g = Enum.valueOf(Gender.class, "MALE");
//g.name = "nan";
g.setName("男");
System.out.println(g + " Express " + g.getName());
g.setName("女");
}
}
最终的修改版本:枚举类通常应该被设计为不可改变的类,也就是说他的成员变量应该不可改变,建议将成员变量都用private final修饰:
//Gender.java
public enum Gender {
//这里的男和女赋值,是直接调用了构造,只不过枚举类中的构造不需要new关键字,也不需要显示的构造调用
MALE("男"),FEMALE("女");
//定义为私有不可变
private final String name;
//类的构造
private Gender(String name){
this.name = name;
}
public String getName(){
return this.name;
}
}
//GenderTest.java
public class GenderTest {
public static void main(String[] args) {
//通过valueOf获取枚举类的枚举值
Gender g = Enum.valueOf(Gender.class, "MALE");
System.out.println(g + "的枚举值是" + g.getName());
}
}
2.3实现接口的枚举类
//InterfaceGender.java
public interface InterfaceGender {
void Info();
}
//Gender.java
public enum Gender implements InterfaceGender{
//这里的男和女赋值,是直接调用了构造,只不过枚举类中的构造不需要new关键字,也不需要显示的构造调用
MALE("男"),FEMALE("女");
//定义为私有不可变
private final String name;
//类的构造
private Gender(String name){
this.name = name;
}
public String getName(){
return this.name;
}
//接口方法
@Override
public void Info() {
// TODO Auto-generated method stub
System.out.println("接口的方法");
}
}
//GenderTest.java
public class GenderTest {
public static void main(String[] args) {
//通过valueOf获取枚举类的枚举值
Gender g = Enum.valueOf(Gender.class, "MALE");
System.out.println(g + "的枚举值是" + g.getName());
g.Info();
}
}
//Gender.java
public enum Gender implements InterfaceGender{
//这里的男和女赋值,是直接调用了构造,只不过枚举类中的构造不需要new关键字,也不需要显示的构造调用
//并且用了匿名内部类来实现Info方法的重写
MALE("男"){
public void Info(){
System.out.println("男性的重写方法实现");
}
},
FEMALE("女"){
public void Info(){
System.out.println("女性的重写方法实现");
}
};
//定义为私有不可变
private final String name;
//类的构造
private Gender(String name){
this.name = name;
}
public String getName(){
return this.name;
}
}
main方法中的实现不变
2.4包含抽象方法的枚举类
//Calculate.java
public enum Calculate {
PLUS{
public double CalculateNum(double a, double b){
return a + b;
}
},
MINUS{
public double CalculateNum(double a, double b){
return a - b;//do not check num
}
},
TIMES{
public double CalculateNum(double a, double b){
return a * b;
}
},
DIVIDE{
public double CalculateNum(double a, double b){
return a / b;//do not check num
}
};
public abstract double CalculateNum(double a, double b);
}
//CalculatePrint.java
public class CalculatePrint {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(Calculate.PLUS.CalculateNum(1, 2));
System.out.println(Calculate.MINUS.CalculateNum(1, 2));
System.out.println(Calculate.TIMES.CalculateNum(1, 2));
System.out.println(Calculate.DIVIDE.CalculateNum(1, 2));
}
}