我有个任务,在那里我应该做个麻将。在那个游戏中,我必须包括多态。
我想在我的子类蛇中运行这个方法,但是当我尝试从我的游戏类访问它时,它在父类怪物中运行该方法,而不是在子类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);
}
}-->
public void fightMonsters(Monsters variabel){
variabel.fight();
},但这个goes方法指的是我的父类版本的fight(),而不是子类版本?。
发布于 2017-10-14 17:53:18
方法签名
您没有调用正确的方法:最后,您的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>)不存在。一种选择是将您的怪物声明为抽象类:
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方法。
发布于 2017-10-14 17:48:02
方法重载只有在重载和重载方法具有相同签名的情况下才能工作,这意味着相同的参数类型和相同的返回类型。
Snake中的方法有两个参数,而Monsters中的方法根本没有参数,因此没有重载。
当您调用Snake.fight()时,会搜索这样的方法,但找不到(没有方法被称为fight(),并且接受零参数),因此调用父方法。
奖励:
Monsters类看起来像一个简单的父类,用于所有的怪物类型,比如蛇、骨架、妖精等等,这些类永远不应该被实例化。在这种情况下,最好让它成为abstract,完全没有方法实现,所以您不会错误地调用它的方法。
发布于 2017-10-14 17:50:26
Java中的多态性是对象具有不同形式的能力,例如,类调用不同的方法。
编译器可以根据传递的参数的数量(和类型)在这两个fight方法之间进行选择,因为它们是不同的。
Snake.fight(Player, ArrayList<String>)有2个参数,Monster.fight()有0参数。
https://stackoverflow.com/questions/46747484
复制相似问题