码迷,mamicode.com
首页 > 编程语言 > 详细

Java enum

时间:2016-01-30 01:49:53      阅读:208      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!