多态性指的是子类

时间:2017-10-14 17:41:10

标签: java polymorphism

我有一个任务,我应该做一个迷宫游戏。在那个游戏中,我必须包含多态性。

我想在我的子类Snake中运行该方法,但是当我尝试从我的游戏类访问它时,它运行父类Monsters中的方法而不是子类Snake中的方法

由于我的整个代码真的很混乱,我只会发布必要的代码,如果需要我可以发布更多的上下文。

我的父类:

package tag1;
import textio.SysTextIO;
import textio.TextIO;

public class Monsters {

    TextIO io = new TextIO(new SysTextIO());

    public void fight(){
        io.put("You have encountered a Monster, will you fight it for a 
potential reward?");   
    }

}

我的子类 - 此类中的方法是我想要运行的方法

package tag1;
import java.util.ArrayList;
public class Snake extends Monsters {

public void fight(Player p, ArrayList<String>currentInventory) {


    boolean condition = true;
    String choice;
    io.put("You have encountered a resting SNAKE, will you fight it for a 
potential reward? Y/N \n");
    choice = io.get();
    while (condition) {
        switch (choice) {
            case "y":
                io.put("The snake seems to realize your intentions, it looks like it is going to bite, \n"
                        + " will you punch it or stomp on it? type P/S \n");
                choice = io.get();
                if (choice.equals("P")) {
                    io.put("You got too close, and the snake bit you! , and you lost 20 health!\n");
                    p.setHealth(p.getHealth()-20);
                    io.put("you Currently have " + p.getHealth());
                    //Alternativt kan man give personen et vigtigt ITEM senere i spillet, med et andet
                    //dyr fra Monster superklasssen
                } else if(choice.equals("S")){
                    io.put("You succesfully killed the snake, with your stomp, and discover"
                            + "a healthpotion underneath the snake!\n");
                    currentInventory.add("healtpotion");
                    io.put("Healthpotion added to inventory - to use healthpotion type HP");
                }   condition = false;
                break;
            case "n":
                io.put("you silently move around, as the snake rests \n");
                condition = false;
                break;
            default:
                io.put("Seems like you have entered something invalid, type Y / N \n");
                choice = io.get();
                break;
        }

        }
    }
}

在我的游戏课中我有这个方法

public void monsterFight(Player p, ArrayList<Room> roomList){
    if (p.getCurrentRoom().equals(roomList.get(1))) {
       fightMonsters(snakeObject);
    }
}

- &GT;

public void fightMonsters(Monsters variabel){
    variabel.fight();
}

但是这个方法是指我的父类版本的fight()而不是子类版本?

3 个答案:

答案 0 :(得分:3)

方法重载仅在重载和重载方法具有相同签名时才起作用,这意味着相同的参数类型和相同的返回类型。

Snake中的方法有两个参数,而Monsters中的方法根本没有参数,所以它没有重载。

当你调用Snake.fight()时,搜索但找不到这样的方法(没有方法被称为fight()并且接受零参数),因此调用父方法。

加成:
Monsters类看起来像一个简单的父类,适用于所有怪物类型,例如蛇,骨架,地精或其他什么,它们永远不应该被实例化。在这种情况下,可能最好将其设为abstract,而根本没有方法实现,因此您不会错误地调用它的方法。

答案 1 :(得分:2)

方法签名

你没有调用正确的方法:最后,你的Snake类有两个战斗方法签名:

  • public void fight()(来自Monster班级)
  • public void fight(Player p, ArrayList<String>currentInventory)(来自Snake班级)

这意味着在monsterFight(Player p, ArrayList<Room> roomList)方法中,您必须更改为:

public void monsterFight(Player p, ArrayList<Room> roomList){
    if (p.getCurrentRoom().equals(roomList.get(1))) {
       // your monster has to fight a player!
       fightMonsters(snakeObject, p);

       // option B is to directly call:
       // snakeObject.fight(p, p.getInventory());
       // with the assumption on getInventory() defined later
    }
}

还有以下变化:

public void fightMonsters(Monsters variabel, Player p){
    variabel.fight(p, p.getInventory());
}

假设getInventory();签名为public List<String> getInventory();。这样,你的怪物正在调用正确的方法。

抽象类

,这仍然是错误的:在Monster课程中,fight(Player, List<String>)不存在。一种选择是将您的Monster声明为抽象类:

public abstract class Monster{
    public void fight(){
        // your method that you define
    }

    // this method has to be overridden in children classes
    public abstract void fight(Player p, List<String> inventory);
}

因此,您的Snake类将如下所示:

public class Snake extends Monster{

    @Override
    public void fight(Player p, List<String> inventory){
        // your method
    }

}

然后,您可以为任何怪物调用fight方法。

答案 2 :(得分:1)

Java中的多态性是对象具有不同形式的能力,例如,类可以调用不同的方法。

编译器可以在两种fight方法之间进行选择,因为它们是不同的,基于传递的参数的数量(和类型)。

Snake.fight(Player, ArrayList<String>)有2个参数,Monster.fight()有0个参数。