Java 涉及子类的多态性

Java 涉及子类的多态性,java,polymorphism,Java,Polymorphism,我有一个任务,我应该在那里做一个迷宫。在那个游戏中,我必须包含多态性 我想在我的子类Snake中运行该方法,但当我尝试从游戏类访问它时,它会在父类怪物中运行该方法,而不是在子类Snake中运行该方法 因为我的整个代码都很混乱,所以我只发布必要的代码,如果上下文需要,我可以发布更多 我的家长班: package tag1; import textio.SysTextIO; import textio.TextIO; public class Monsters { TextIO io =

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

我想在我的子类Snake中运行该方法,但当我尝试从游戏类访问它时,它会在父类怪物中运行该方法,而不是在子类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?");   
    }

}
My childclass-该类中的方法是我要运行的方法

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;
        }

        }
    }
}

但是这个goes方法引用了fight()的父类版本,而不是子类版本?

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

Snake
中的方法有两个参数,而
Monsters
中的方法根本没有参数,因此没有重载

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

奖金:

怪物
类看起来像是所有怪物类型的简单父类,例如蛇、骷髅、地精或其他怪物,它们永远不应该被实例化。在这种情况下,最好将其
抽象化
,完全没有方法实现,这样就不会错误地调用它的方法

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

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

Snake.fight(玩家,数组列表)
有2个参数,
Monster.fight()
有0个参数。

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

  • public void fight()
    (来自
    Monster
    职业)
  • 公开空战(玩家p,ArrayListcurrentInventory)
    (来自
    蛇类)
这意味着在你的
怪物战斗(玩家p,ArrayList 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
    }
}
假设
getInventory()签名是
公共列表getInventory()。这样,你的怪物正在调用正确的方法

抽象类 然而,这仍然是不正确的:在
怪物
类中,
战斗(玩家,列表)
不存在。一个选项是将怪物声明为抽象类:

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);
}
公共抽象类怪物{
公众虚空斗争(){
//您定义的方法
}
//必须在子类中重写此方法
公开摘要无效战斗(玩家p,列表清单);
}
因此,您的Snake类将如下所示:

public class Snake extends Monster{

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

}
公共类Snake扩展怪物{
@凌驾
公共无效战(玩家p,名单清单){
//你的方法
}
}

然后,你可以为任何怪物调用
战斗
方法。

欢迎你!谢谢你接受我的回答!如果你认为它们是有用的,请考虑其他答案。PS:抽象类的胜利!
public void fightMonsters(Monsters variabel, Player p){
    variabel.fight(p, p.getInventory());
}
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);
}
public class Snake extends Monster{

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

}