静态引用非静态方法错误

时间:2016-01-31 20:56:21

标签: java

我无法弄清楚我在以下代码段中做错了什么:

int quantity;
String color;
String flower;//the type of flower associate with the color

System.out.print("Please enter a color: ");
    color = input.next();
    System.out.print("Please enter the quantity: ");
    quantity = input.nextInt();

    switch(color){
    case red:
        System.out.println("You have one rose.");
        break;
    case blue:
        System.out.println("You have 2 blue irises.");
        break;

我收到以下错误:

private Door openOtherDoor(Door prizeDoor, Door selectedDoor){
Door.setPrize(prizeDoor.doorPrize);
Door.open(selectedDoor.doorPrize);

非常感谢任何帮助

2 个答案:

答案 0 :(得分:-1)

如果这些方法属于Door类,那么你可以做

private Door openOtherDoor(Door prizeDoor, Door selectedDoor){
Door.setPrize(prizeDoor.doorPrize);
Door.open(selectedDoor.doorPrize);

如果setPrize是静态方法,否则您将需要实例方法,即

private Door openOtherDoor(Door prizeDoor, Door selectedDoor){
    this.setPrize(prizeDoor.doorPrize);
    this.open(selectedDoor.doorPrize);

答案 1 :(得分:-1)

根据错误。您应该将Door类中的open方法定义为static。