使用多态在方法中调用方法?

时间:2015-03-03 04:58:12

标签: java

我需要使用多态来调用play()方法中的move()方法。我不知道该怎么做。我的程序中的其他所有内容都能正常工作我唯一的问题是如何在play()方法中使用move()方法。 Move()方法在另一个名为“Humans”的类中,它也使用“播放器”界面。该方法正常工作。

public interface Players {
    int move();   // returns an int that is given by user    
}
// Method that I need call the move() method in
// You may ignore the rest of the code expect where I commented 
class Nim implements Players {
    private int marbles = 0;
    private int take = 0;
    public void play() {
        playerName = obj2.getName();
        System.out.println("Player " + playerName);
        turn();
        getComp();
        int removeMarbles = 0;
        System.out.println("\nNumber of marbles: " + marbles);
        Pile obj = new Pile();
        while (marbles > 0) {
            if (turn % 2 != 0) {
                take = move() // need to call move method using polymorphism, ***code does not work just to show where I need help
                System.out.println(playerName + " took " + take + " marble(s)");
                marbles = marbles - take;
                System.out.println("There are " + marbles + " marbles left.");
            }

            if (turn % 2 == 0) {
                if (compName.equals("Dumb")) {
                    removeMarbles = (int)(Math.random() * (marbles / 2) + 1);
                    System.out.println("\nThe computer took " + removeMarbles + " marble(s)");
                    marbles -= removeMarbles;
                }

                if (compName.equals("Smart")) {
                    int pile = marbles;
                    int power = 2;
                    if (marbles <= power) {
                        removeMarbles = 1;
                    }
                    if (marbles > power) {
                        while (power < marbles) {
                            power = power * 2;
                        }
                        removeMarbles = ((power / 2) - 1);
                    }
                    marbles = removeMarbles;
                    System.out.println("\nThe computer took " + (pile - removeMarbles) + " marble(s)");
                }
                System.out.println("There are " + marbles + " marbles left.\n");
            }

            if (marbles == 0) {
                if (turn % 2 != 0) {
                    System.out.println("\nThe computer is the winner!!!!");
                } else {
                    System.out.println("\n" + playerName + " is the winner!!!!");
                }
            }
            turn++;
        }
    }
}

3 个答案:

答案 0 :(得分:0)

Interfaces are contracts describing the group of related methods with empty bodies.您需要在子类中实现move()方法,然后使用任何其他功能覆盖该方法:

public int move(){

  // Implements interface .move();
   System.out.println("Nim .move()");
}

来自Docs

如果要在Players中实现该方法,则无法将其实现为接口,因为实现该接口的方法将覆盖该行为。您应该考虑使用super关键字的抽象类结构:

来自Docs

答案 1 :(得分:0)

首先你需要在Humans类中实现该方法,如下所示

public class Humans implements Players {
   public int move(){
       // your logic here
   }
}

然后您可以使用对象调用该方法,如下所示

Players  ob = new Humans();
take = ob.move();

答案 2 :(得分:-1)

目前,您的move()被声明为变量。你想在接口中使它成为一个方法,所以你可以不用强制转换来调用它。

基本上,

int move();

应该看起来像:

public int move(){
  // your code here;
}