zoukankan      html  css  js  c++  java
  • JAVA的枚举类

         到目前为止,我们仅仅使用了最简单的语法定义枚举类型,其实枚举类型可以做更多的事情,在Tiger的定义中,枚举是一种新的类型,允许用常量来表示特定的数据片断,它能胜任普通类的大部分功能,如定义自己的构造函数、方法、属性等等。这也是Java与C/C++或是Pascal中不同的地方,在那两种语言中枚举类型代表的就是一些int类型的数字,但在Java中枚举更像是一个类。

       

     1 import java.util.*;
     2 
     3 public class EnumTest {
     4 
     5     /**
     6      * @param args
     7      */
     8     public static void main(String[] args) {
     9         // TODO Auto-generated method stub
    10         Scanner in=new Scanner(System.in);
    11         System.out.print("Enter a size: (SMALL,MEDIUM,LARGE,EXTRA_LARGE)");
    12         String input=in.next().toUpperCase();
    13         Size size=Enum.valueOf(Size.class, input);
    14         System.out.println("size="+size);
    15         System.out.println("abreviation="+size.getAbbreviation());
    16         if (size==Size.EXTRA_LARGE)
    17             System.out.println("Good job--you paid attention to the _.");
    18     }
    19 }
    20 
    21 enum Size
    22 {
    23     SMALL("S"),MEDIUM("M"),LARGE("L"),EXTRA_LARGE("XL");
    24     private Size(String abbreviation) {this.abbreviation=abbreviation;}
    25     public String getAbbreviation() {return abbreviation;}
    26     private String abbreviation;
    27 }
  • 相关阅读:
    hadoop balance
    随笔
    ubuntu server 使用parted分区
    程序员内功续
    hadoop——hdfs多硬盘挂载
    hdfs老数据压缩备份的一些问题20120521
    hadoop balance failed
    hoj 2524 Allocate Dormitories 二分图的最大匹配
    HDOJ 分类(转)
    hoj 3008 Matryoshka Dolls Again 最大独立子集
  • 原文地址:https://www.cnblogs.com/djcsch2001/p/2485109.html
Copyright © 2011-2022 走看看