Java 在中调用超级方法时忽略子类方法代码
我正在创建一个基于文本输入的程序。 每次接收到输入时,都会有基于文本(系统打印)的输出和使用JFrame的图形表示输出 我有一个类Java 在中调用超级方法时忽略子类方法代码,java,inheritance,methods,Java,Inheritance,Methods,我正在创建一个基于文本输入的程序。 每次接收到输入时,都会有基于文本(系统打印)的输出和使用JFrame的图形表示输出 我有一个类Player,它扩展了UiPlayer UiPlayer中的方法围绕UI二维地图控制播放器。Player中的方法更改变量以显示基于文本的输出。 播放器中的相关代码: public class Player extends UiPlayer { private MyDirection facing; public Player(.....) //irrel
Player
,它扩展了UiPlayer
UiPlayer
中的方法围绕UI二维地图控制播放器。Player
中的方法更改变量以显示基于文本的输出。
播放器中的相关代码
:
public class Player extends UiPlayer
{
private MyDirection facing;
public Player(.....) //irrelevant arguments
{
super(.....);
facing = MyDirection.EAST;
}
/**
* make the player turn right, both in text and in GUI window
*/
public void turnRight()
{
super.turnRight();
if(facing == MyDirection.NORTH)
{facing = MyDirection.EAST;}
else if(facing == MyDirection.WEST)
{facing = MyDirection.NORTH;}
else if(facing == MyDirection.SOUTH)
{facing = MyDirection.WEST;}
else
{facing = MyDirection.SOUTH;}
}
/**
* make the player turn 180 degrees, both in text and on graphics window
*/
public void turnAround()
{
super.turnAround();
if(facing == MyDirection.NORTH)
{facing = MyDirection.SOUTH;}
else if(facing == MyDirection.WEST)
{facing = MyDirection.EAST;}
else if(facing == MyDirection.SOUTH)
{facing = MyDirection.NORTH;}
else
{facing = MyDirection.WEST;}
}
然而,出于某种原因,尽管turnRight()
和turnLeft()
工作得很好,但当调用turnRevolution()
时,如果其中包括调用超级方法,则调用超级方法,播放器将打开GUI和所有其他代码以更改基于文本的方向(即面向
)似乎被忽视了
如果不包括super方法,那么face
的值每次都会发生变化,非常完美
我尝试过重命名这些方法,并将super.turnaver()
放在if语句之后
编辑:另外,我没有UiPlayer的代码,我只有方法文档。它表示| void | Turn()|将玩家转向,使其面向相反的方向
有什么想法吗?> 如果我是你,你不能让周转
工作,你没有权限修复代码和类,看到底发生了什么
然后我会打两次电话给左转
或右转
,如果你仔细考虑的话,这基本上就是转身
这不是一个超级优化的方法来处理它或其他什么,但它应该可以解决你的问题:)你能添加超级方法吗?我猜super.turnarl()和Player.turnarl()做同样的事情,结果是玩家转了两圈,结果是朝着同一个方向。你定义了
turn
method的UiPlayer类是什么?我没有超级方法的代码,只有它将玩家转向相反方向的文档/