传递枚举参数

时间:2011-07-16 20:26:15

标签: java

我有一个问题从我的main传递枚举值到我的一个类。我的程序存储有关书呆子的信息:name,nerdfactor。它有一系列类和子类。我有一个超类,它扩展到另外两个类和第三个类,它扩展到前面提到的子类之一。我的程序是supost要做的是采取填充的参数,如名称,nerdfactor,并且书呆子喜欢WII,XBOX或PS3。 Bellow是我主要的剪辑和我的子类的开头。如果有人可以帮我弄清楚如何通过枚举的论点,我将非常感激。

public static void main(String[] args) {

    // List to keep a few nerds in
    ArrayList<GenericNerd> nerdList = new ArrayList(10);
    GenericNerd nerd = new GenericNerd("Bugger McNosePicker", 9);
    nerdList.add(nerd);

    nerd = new ConsoleGeek("Freddy where's my mommy", 9, WII); // the WII should be an enum value
    nerdList.add(nerd);
}

Subclass ConsoleGeek

public class ConsoleGeek extends GenericNerd {

    enum ConsoleType {

        WII, PS3, XBOX
    };
    private ConsoleType console;

    public void setConsoleType(ConsoleType console) {
        this.console = console;
    }
    public ConsoleType getConsoleType(){
    return console;
}


public ConsoleGeek(String name, int nerdFactor, ConsoleType console) {
    super(name, nerdFactor);
    if (name == null) {
        System.out.println("Error: GenericNerd constructor - name is null!");
        System.exit(1);
    }
       // the setNerdFactor method handles parameter range checking
    setNerdFactor(nerdFactor);

    this.name = name;
    if (console == ConsoleType.WII) {

        // make sure WII players nerd factor is less than 6!
        if (nerdFactor > 5) {
            System.out.println("Warning: " + name
                    + " Prefers WII! (setting nerdFactor to 5).");
            setNerdFactor(5);
        }
        // Make sure XBOX players nerd factor is between 3 and 8
    } else if (console == ConsoleType.XBOX) {
        if (nerdFactor < 3 || nerdFactor > 8) {
            System.out.println("Warning: " + name
                    + " Prefers XBOX! (setting nerdFactor to 8).");
            setNerdFactor(8);
        }

    }
}

我想到了制造像

这样的新型书呆子的想法
ConsoleGeek supergeek = new ConsoleGeek(arguments)

但我很确定它应该保留其他格式。

1 个答案:

答案 0 :(得分:2)

nerd = new ConsoleGeek("Freddy where's my mommy", 9, ConsoleGeek.ConsoleType.WII);