首页 > 编程语言 > 详细

Java enum

时间:2016-01-30 01:47:33      阅读:168      评论:0      收藏:0      [点我收藏+]

An enum type is a special data type that enables for a variable to be a set of predefined constants. The constructor of enum must be private. It means the same thing as making it package private. The only way to instantiate an enum is by declaring them within your enum class. Enums cannot have public constructors.

package Enum;

public class FoodEnumDemo {

    public enum Food {
        HAMBURGER(7), FRIES(2), HOTDOG(3), ARTICHOKE(4);

        Food(int price) {
            this.price = price;
        }

        private final int price;

        public int getPrice() {
            return price;
        }
    }

    public static void main(String[] args) {
        for (Food f : Food.values()) {
            System.out.print("Food: " + f + ", ");

            if (f.getPrice() >= 4) {
                System.out.print("Expensive, ");
            } else {
                System.out.print("Affordable, ");
            }

            switch (f) {
            case HAMBURGER:
                System.out.println("Tasty");
                break;
            case ARTICHOKE:
                System.out.println("Delicious");
                break;
            default:
                System.out.println("OK");
            }
        }

    }

}

 

Java enum

原文:http://www.cnblogs.com/touchdown/p/5169964.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!