在内部调用超级方法时子类方法代码被忽略
Subclass method code being ignored when calling super method within
我正在创建一个基于文本输入的程序。
每次收到输入时,都会有基于文本(系统打印)的输出和使用 JFrame 的图形表示输出。
我有扩展 UiPlayer
的 class Player
。
UiPlayer
中的方法控制 UI 二维地图周围的玩家。 Player
中的方法更改变量以显示基于文本的输出。
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()
工作完美,但是在调用turnAround()
时,如果包含调用super方法,则调用super方法,玩家上交 GUI 和所有其他用于更改基于文本的方向(即 facing
)的代码似乎都被忽略了。
如果我不包括 super 方法,facing
的值每次都会完美地改变。
我试过重命名方法,并在 if 语句后放置 super.turnAround()
。
编辑:另外,我无权访问 UiPlayer 的代码,我只有方法文档。它指出 |无效 |转身() |转动播放器使其面向相反的方向。
有什么想法吗? ><
如果我是你,你无法让 turnAround
工作并且你无权修复代码和class 看看到底发生了什么,
然后我会调用 两次 turnLeft
或 turnRight
如果你考虑的话基本上是 turnAround
..
这不是处理它或其他任何东西的超级优化方法,但它应该可以解决您的问题:)
我正在创建一个基于文本输入的程序。 每次收到输入时,都会有基于文本(系统打印)的输出和使用 JFrame 的图形表示输出。
我有扩展 UiPlayer
的 class Player
。
UiPlayer
中的方法控制 UI 二维地图周围的玩家。 Player
中的方法更改变量以显示基于文本的输出。
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()
工作完美,但是在调用turnAround()
时,如果包含调用super方法,则调用super方法,玩家上交 GUI 和所有其他用于更改基于文本的方向(即 facing
)的代码似乎都被忽略了。
如果我不包括 super 方法,facing
的值每次都会完美地改变。
我试过重命名方法,并在 if 语句后放置 super.turnAround()
。
编辑:另外,我无权访问 UiPlayer 的代码,我只有方法文档。它指出 |无效 |转身() |转动播放器使其面向相反的方向。
有什么想法吗? ><
如果我是你,你无法让 turnAround
工作并且你无权修复代码和class 看看到底发生了什么,
然后我会调用 两次 turnLeft
或 turnRight
如果你考虑的话基本上是 turnAround
..
这不是处理它或其他任何东西的超级优化方法,但它应该可以解决您的问题:)