【JAVA】浅谈java枚举类

一、什么情况下使用枚举类?

  有的时候一个类的对象是有限且固定的,这种情况下我们使用枚举类就比较方便?

二、为什么不用静态常量来替代枚举类呢?

    public static final int SEASON_SPRING = 1;
public static final int SEASON_SUMMER = 2;
public static final int SEASON_FALL = 3;
public static final int SEASON_WINTER = 4;

  枚举类更加直观,类型安全。使用常量会有以下几个缺陷:

  1. 类型不安全。若一个方法中要求传入季节这个参数,用常量的话,形参就是int类型,开发者传入任意类型的int类型值就行,但是如果是枚举类型的话,就只能传入枚举类中包含的对象。

  2. 没有命名空间。开发者要在命名的时候以SEASON_开头,这样另外一个开发者再看这段代码的时候,才知道这四个常量分别代表季节。

三、枚举类入门

  先看一个简单的枚举类。

package enumcase;

public enum SeasonEnum {
SPRING,SUMMER,FALL,WINTER;
}
  1. enum和class、interface的地位一样
  2. 使用enum定义的枚举类默认继承了java.lang.Enum,而不是继承Object类。枚举类可以实现一个或多个接口。
  3. 枚举类的所有实例都必须放在第一行展示,不需使用new 关键字,不需显式调用构造器。自动添加public static final修饰。
  4. 使用enum定义、非抽象的枚举类默认使用final修饰,不可以被继承。
  5. 枚举类的构造器只能是私有的。

四、枚举类介绍

  枚举类内也可以定义属性和方法,可是是静态的和非静态的。

package enumcase;

public enum SeasonEnum {
SPRING("春天"),SUMMER("夏天"),FALL("秋天"),WINTER("冬天"); private final String name; private SeasonEnum(String name)
{
this.name = name;
} public String getName() {
return name;
}
}

   实际上在第一行写枚举类实例的时候,默认是调用了构造器的,所以此处需要传入参数,因为没有显式申明无参构造器,只能调用有参数的构造器。

  构造器需定义成私有的,这样就不能在别处申明此类的对象了。枚举类通常应该设计成不可变类,它的Field不应该被改变,这样会更安全,而且代码更加简洁。所以我们将Field用private final修饰。

五、枚举类实现接口

  枚举类可以实现一个或多个接口。与普通类一样,实现接口的时候需要实现接口中定义的所有方法,若没有完全实现,那这个枚举类就是抽象的,只是不需显式加上abstract修饰,系统化会默认加上。

  

package enumcase;

public enum Operation {
PLUS{ @Override
public double eval(double x, double y) {
return x + y;
} },
MINUS{ @Override
public double eval(double x, double y) {
return x - y;
} },
TIMES{ @Override
public double eval(double x, double y) {
return x * y;
} },
DIVIDE{ @Override
public double eval(double x, double y) {
return x / y;
} }; /**
* 抽象方法,由不同的枚举值提供不同的实现。
* @param x
* @param y
* @return
*/
public abstract double eval(double x, double y); public static void main(String[] args) {
System.out.println(Operation.PLUS.eval(10, 2));
System.out.println(Operation.MINUS.eval(10, 2));
System.out.println(Operation.TIMES.eval(10, 2));
System.out.println(Operation.DIVIDE.eval(10, 2));
}
}

  Operatio类实际上是抽象的,不可以创建枚举值,所以此处在申明枚举值的时候,都实现了抽象方法,这其实是匿名内部类的实现,花括号部分是一个类体。我们可以看下编译以后的文件。

  aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgwAAABqCAIAAABf1p42AAAgAElEQVR4nO1dS3PbRrbuZbLIz4lL/AdJKv8gVXdspbDELg//AIuYOC5KqVuzuQktrmeoxAxMC2QkOXehSSKPxZo8FF3FFUl2ZKnmznVoiQQfaJB30USjH6fBt0Qp56uzIA66Dw5g1/nQ3VB/5D//8pd//etflNKdozpFIBAIBEIAQZJAIBAIhAmjk0Q7oD8fB//9S+DtoKGhoaFdTRudJL7/Ldg5pmfNkIYdNDQ0NLQraaOTxFe7QSvotIOQ29dmbGxslMpfiY3R0NDQ0GbfRicJbyegYacVhNy+/vprGgSUBmFIw5B2OmGnE1IadDrh1taW53le+SuxPRoaGhrajNtYJBFQiSQePnwYBG2dJzqdcHNz0/f91dXV8lfrF37PaGhoaGgD2lgk0aadZjvktr6+3m63QJ4ol8ue53meVy6XxS4XbDuLcyR1Z2e07nt3UlZh9KsX3yXjdEdDQ7twO36Qtu9uG85u59Krx+Khfbdy0QmPYuOSRKMdcltbW2u3mu12K2i3adBWeOL50dHB/n65XBa7xLazOEc4Uh/vQG0mbjuLcyNeq/gumOqXFiHk3S8HjGDdO4d7RDt/+62UtnvIbnN/JdvzOcXfhMbbOblZ+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BcHvqGIYkEqbnqEFIKEldiLcAiYfSiucdoegQiJWVFSZc2qH73e5J0H3R7Z483XlACAmiT2NXVla0fslrEmaSSBp/8CNRSXQokuDa1CORBHxpIS1NohWp4ioBkFWOy0ifNQkKdTNxhHhkDpTwBc7IEy6CKPSFkITxUvpj0JcFwLm14Z9ELzCUBiMJaEHi0i1KTJIk8vk8Ey4lGnqfPAXtfD6vdxQrKj+WpvaBA4laXAtiFKGICx0Hm27qHUAkEc87iTm4GXFNArz0biajaHHrHsSlBzjnIlQutbhpc1GicjLEEfIVJMFmqY3wjQ5UlYx5qk7ug2PCP88B7OMwkaDgr5sAxo7WC6RZM/Ue+1NexAHwx1Q4ktCRzWbPTk8jgeuaLFzaZDyRzWbhzsa/k9jNpIhlQevWwJovMOzoRbQsiRqGWrg2koR4BU42MRXpl9bX38EVecQlhvryqM7mCyVF/Z5S+LxUft/UPrkRe8brAcY2iX/wIF4MdCosAMS8sL+SkBIWFkbEO/C04YDEq1rmoHL3SYUAAABwSURBVBO+rEjntkb9SBI6lpaWuMB1HRK4breaS0tLQ0bFV2zEHx0ocj0zSFqq6J3jc39IEjrS6fRptcp5ogbxRDqdHjIqkgTiDw7kCMRFYpIkcXMwDBkVSQKBQCAuDCg6hEAgEAgjkCQQCAQCYcT/A/wDd+mMkAS3AAAAAElFTkSuQmCC" alt="" />

  共生成了五个class文件,这样就证明了PLUS,MINUS,TIMES,DIVIDE是Operation的匿名内部类的实例。

六、switch语句里的表达式可以是枚举值

  Java5新增了enum关键字,同时扩展了switch。

package enumcase;

public class SeasonTest {
public void judge(SeasonEnum s)
{
switch(s)
{
case SPRING:
System.out.println("春天适合踏青。");
break;
case SUMMER:
System.out.println("夏天要去游泳啦。");
break;
case FALL:
System.out.println("秋天一定要去旅游哦。");
break;
case WINTER:
System.out.println("冬天要是下雪就好啦。");
break;
}
} public static void main(String[] args) {
SeasonEnum s = SeasonEnum.SPRING;
SeasonTest test = new SeasonTest();
test.judge(s);
}
}

  case表达式中直接写入枚举值,不需加入枚举类作为限定。

上一篇:【CF497E】Subsequences Return 矩阵乘法


下一篇:洗礼灵魂,修炼python(86)--全栈项目实战篇(12)—— 利用socket实现文件传输/并发式聊天