Input 的定义:
//: enumerated/Input.java package enumerated; import java.util.*; public enum Input { NICKEL(5), DIME(10), QUARTER(25), DOLLAR(100), TOOTHPASTE(200), CHIPS(75), SODA(100), SOAP(50), ABORT_TRANSACTION { public int amount() { // Disallow throw new RuntimeException("ABORT.amount()"); } }, STOP { // This must be the last instance. public int amount() { // Disallow throw new RuntimeException("SHUT_DOWN.amount()"); } }; int value; // In cents Input(int value) { this.value = value; } Input() {} int amount() { return value; }; // In cents static Random rand = new Random(47); public static Input randomSelection() { // Don't include STOP: return values()[rand.nextInt(values().length - 1)]; } } ///:~ Category 的定义:
package chapter19; import java.util.EnumMap; //import net.mindview.util.*; import static enumerated.Input.*; //import static net.mindview.util.Print.*; enum Category { MONEY(NICKEL, DIME, QUARTER, DOLLAR), ITEM_SELECTION(TOOTHPASTE, CHIPS, SODA, SOAP), QUIT_TRANSACTION(ABORT_TRANSACTION), SHUT_DOWN(STOP); private Input[] values; Category(Input...types) { values = types; } private static EnumMap<Input, Category> categories = new EnumMap<Input, Category>(Input.class); static { for(Category c : Category.class.getEnumConstants()) for(Input type : c.values) categories.put(type, c); } public static Category categorize(Input input) { return categories.get(input); } } 在 Category 中声明实例时有错误,每个实例名下面都带下划线,错误是: The constructor Category(Input, Input, Input, Input) is undefined 但是我明明定义了啊,请问是什么问题
