[英]Cannot cast class A to class B
What is the proper way to access the field level
from instance of Minotaur
? 从
Minotaur
实例访问字段level
的正确方法是什么? Getting error in for (int i =0 ; i < ((Monster) this).level ; i++)
which is Cannot cast from Player to Monster
. Cannot cast from Player to Monster
获取for (int i =0 ; i < ((Monster) this).level ; i++)
,该错误Cannot cast from Player to Monster
。
package player;
import monsters.*;
public class Player extends GameCharacter {
public void performAttackOn(GameCharacter who){
if (who instanceof Monster ){
for (int i =0 ; i < ((Monster) this).level ; i++) { // <<
}
public static class Minotaur extends Monster{ // << nested class which is being created and there is need to access it's level
public Minotaur () {
type = "Minotaur";
}
}
}
package monsters;
public class Monster extends GameCharacter{
public int level = 1;
}
package monsters;
public abstract class GameCharacter{
public static class Minotaur extends Monster{
public Minotaur(){
type = "Minotaur";
hitPoints = 30;
weapon = new Weapon.Sword();
}
}
}
Minotaur
should extend monsters.GameCharacter
, monsters.Monster
and override in Player.player
some methods which was inherited from monsters.GameCharacter
Minotaur
应该扩展monsters.GameCharacter
, monsters.Monster
并在Player.player
重写一些从monsters.GameCharacter
继承的方法。
Have the GameCharacter
interface define a getLevel()
method which Monster
and Player
both implement. 让
GameCharacter
接口定义一个Monster
和Player
都实现的getLevel()
方法。
public interface GameCharacter
{
public int getLevel( );
}
Once you've done that, you're taking advantage of polymorphism and don't even need to cast. 完成此操作后,您就可以利用多态性 ,甚至不需要强制转换。
Also, do you really mean to cast this
which is of type Player
, to type Monster
? 另外,您是否真的要
this
类型转换为Player
类型,将其转换为Monster
类型? Or do you mean: 还是你的意思是:
public void performAttackOn(GameCharacter who)
{
for (int i =0 ; i < who.getLevel( ) ; i++)
{
// do stuff...
}
}
In the hierarchy you have defined, a Player
can never be a Monster
so the cast is impossible. 在您定义的层次结构中,
Player
永远不可能是Monster
因此无法施展角色。
I believe the line you are having issues with, based on the check above it, should reference who
instead of this
: 我认为,根据上面的检查,您遇到问题的那一行应该参考
who
而不是this
:
for(int i = 0; i < ((Monster)who).level; i++)
Thats pretty easy. 那很简单。 You check if
who
is a Monter but if yes, you cast this
to Moster and this
refers to object in witch you currently are, it means some Player. 您检查
who
是蒙特,但如果是的,你投this
来穆斯特和this
是指在巫婆反对你现在是,这意味着一些球员。 replace this 取代这个
((Monster) this).level
with this 有了这个
((Monster) who).level
and the error should dissaper. 错误应该消失。
EDIT: Now I see, you can't have inheritance on fields of classes. 编辑:现在我知道,您不能在类的字段上继承。 You should have a common method
你应该有一个通用的方法
pubic int level();
in your GameCharacter
. 在您的
GameCharacter
。 Then it's your choice if to define level
as two different fields of children classes (not recommended) or just leave to GameCharacter
to have a level field. 然后,您可以选择将
level
定义为子类别的两个不同字段(不建议使用),或者只是留给GameCharacter
来拥有关卡字段。
here: ((Monster) this).level
you are casting the this
instance, which is a Player
to Monster
. 此处:
((Monster) this).level
您正在投射this
实例,它是Monster
的Player
。 That's why you get the error. 这就是为什么您得到错误。
Maybe you meant: 也许你的意思是:
if (who instanceof Monster ){
for (int i =0 ; i < ((Monster) who).level ; i++) { // <<
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.