Java 面向对象(七)

枚举

枚举的引入(模拟枚举)

class Student {
    private int restDay;

    public int getRestDay() {
        return restDay;
    }

    public void setRestDay(int restDay) {
        this.restDay = restDay;
    }
}

public class Test {
    public static void main(String[] args) {
        Student stu = new Student();
        stu.setRestDay(1);
        int res = stu.getRestDay();
        if (res == 6 || res == 7) {
            System.out.println("放假");
        } else {
            System.out.println("上课");
        }
    }
}

上面的代码,存在一些问题:

(1)数据不安全,输入8

stu.setRestDay(8);  // 输出:上课(没有星期八)

(2)业务逻辑不合理:中外一周的第一天不一样,中国第一天是周一,外国第一天是周天。

改进后:

class WeekDay {
    public static final int MONDAY = 1;
    public static final int TUESDAY = 2;
    public static final int WEDNESDAY = 3;
    public static final int THURSDAY = 4;
    public static final int FRIDAY = 5;
    public static final int SATURDAY = 6;
    public static final int SUNDAY = 7;
}

class Student {
    private int restDay;

    public int getRestDay() {
        return restDay;
    }

    public void setRestDay(int restDay) {
        this.restDay = restDay;
    }
}

public class Test {
    public static void main(String[] args) {
        Student stu = new Student();
        stu.setRestDay(WeekDay.MONDAY);
        int res = stu.getRestDay();
        if (res == 6 || res == 7) {
            System.out.println("放假");
        } else {
            System.out.println("上课");
        }
    }
}

改进后的代码,还存在问题:

(1)数据不安全,输入8

stu.setRestDay(8);  // 输出:上课(没有星期八)

再次改进:

class WeekDay {
    private WeekDay() {}
    public static final WeekDay MONDAY = new WeekDay();
    public static final WeekDay TUESDAY = new WeekDay();
    public static final WeekDay WEDNESDAY = new WeekDay();
    public static final WeekDay THURSDAY = new WeekDay();
    public static final WeekDay FRIDAY = new WeekDay();
    public static final WeekDay SATURDAY = new WeekDay();
    public static final WeekDay SUNDAY = new WeekDay();
}

class Student {
    private WeekDay restDay;

    public WeekDay getRestDay() {
        return restDay;
    }

    public void setRestDay(WeekDay restDay) {
        this.restDay = restDay;
    }
}

public class Test {
    public static void main(String[] args) {
        Student stu = new Student();
        stu.setRestDay(WeekDay.MONDAY);
        WeekDay res = stu.getRestDay();
        if (res == WeekDay.SATURDAY || res == WeekDay.SUNDAY) {
            System.out.println("放假");
        } else {
            System.out.println("上课");
        }
    }
}

上面的代码,问题解决了,但是代码不好看,太冗余。

什么是枚举

枚举:表示一个事物固定状态。

比如:季节(春,夏,秋,冬)、星期(周一到周日)、性别(男,女)

枚举的定义

格式:

[修饰符] enum 枚举的名称  {
    常量1,常量2,常量3……
}

比如:

enum Sex {
    MAN, FEMALE
}

枚举的本质

java 枚举是一个语法糖。是一个特殊的类,是多个常量对象的集合。

当我们定义一个枚举后,它的本质还是一个类,一个继承了Enum的类。

enum Sex {
    MAN, FEMALE
}

反编译后:

final class Sex extends Enum
{

    public static final Sex MAN;
    public static final Sex FEMALE;
    private static final Sex ENUM$VALUES[];

    private Sex(String s, int i)
    {
        super(s, i);
    }

    public static Sex[] values()
    {
        Sex asex[];
        int i;
        Sex asex1[];
        System.arraycopy(asex = ENUM$VALUES, 0, asex1 = new Sex[i = asex.length], 0, i);
        return asex1;
    }

    public static Sex valueOf(String s)
    {
        return (Sex)Enum.valueOf(Test/Sex, s);
    }

    static 
    {
        MAN = new Sex("MAN", 0);
        FEMALE = new Sex("FEMALE", 1);
        ENUM$VALUES = (new Sex[] {
            MAN, FEMALE
        });
    }
}

猜你喜欢

转载自www.cnblogs.com/xzh0717/p/11232811.html
今日推荐