如果应该避免向下转型,我应该怎么做?
If Downcasting Should Be Avoided, What Should I Do?
有如下界面class:
public interface IGameObject {
String gameObjectID();
String gameObjectName();
void isActionValid(String action);
void viewActions();
}
我有以下实现上述接口的抽象class。
package gameprobjectpackage;
public abstract class Weapon implements IGameObject {
//Left out getters/setters to keep it simple
private String gameOjectID;
private String gameObjectName;
private int damage;
public Weapon(String gameOjectID, String gameObjectName,int damage) {
super();
this.gameOjectID = gameOjectID;
this.gameObjectName = gameObjectName;
this.damage = damage;
}
我看到一些建议应该避免向下转换的帖子。我明白为什么,但是,我的问题是,如果我需要访问特定于子 class 的方法,我该怎么办。例如:
public class ChargeGun extends Weapon {
private String [] chargeGunActions;
public ChargeGun(String gameOjectID, String gameObjectName, int damage) {
super(gameOjectID, gameObjectName, damage);
chargeGunActions = new String [3];
chargeGunActions[0] = "Charge and Fire";
chargeGunActions[1] = "Release";
chargeGunActions[2] = "Drop Gun";
}
//This method is only meant for gun, and this type of gun is the only one in my game.
//This method, I don't belive should be in the abstract method weapon, because NOT every weapon is a gun.
public void reloadGun()
{
}
我像这样存储在一个中间 hashmap 中:
Map<String,IGameObject> inventory = new HashMap<String,IGameObject>();
当我检索它时,我会得到一个IGameObject
,如何正确转换它,以便我可以访问ChargeGun
中的方法?
您可以使用 visitor pattern 让您免于投射。这个想法很简单:你有一个 IGameObject 清单,它有一个直接调用 v.visit(this) 的方法 accept(GameObjectVisitor v)。在您的 GameObjectVisitor 中,您只需为每个实现实现访问方法:例如访问(Chargegun g)、访问(Sword s)等...
换个说法,就像回旋镖的原理:GameObjectVisitor调用item.accept(this),Item实现accept(GameObjectVisitor g),简单的g.visit(this ).
通过这样做,Visitor 对每个实现都有多个访问方法,并且可以执行特定的操作而无需 cast/using instanceof。
有如下界面class:
public interface IGameObject {
String gameObjectID();
String gameObjectName();
void isActionValid(String action);
void viewActions();
}
我有以下实现上述接口的抽象class。
package gameprobjectpackage;
public abstract class Weapon implements IGameObject {
//Left out getters/setters to keep it simple
private String gameOjectID;
private String gameObjectName;
private int damage;
public Weapon(String gameOjectID, String gameObjectName,int damage) {
super();
this.gameOjectID = gameOjectID;
this.gameObjectName = gameObjectName;
this.damage = damage;
}
我看到一些建议应该避免向下转换的帖子。我明白为什么,但是,我的问题是,如果我需要访问特定于子 class 的方法,我该怎么办。例如:
public class ChargeGun extends Weapon {
private String [] chargeGunActions;
public ChargeGun(String gameOjectID, String gameObjectName, int damage) {
super(gameOjectID, gameObjectName, damage);
chargeGunActions = new String [3];
chargeGunActions[0] = "Charge and Fire";
chargeGunActions[1] = "Release";
chargeGunActions[2] = "Drop Gun";
}
//This method is only meant for gun, and this type of gun is the only one in my game.
//This method, I don't belive should be in the abstract method weapon, because NOT every weapon is a gun.
public void reloadGun()
{
}
我像这样存储在一个中间 hashmap 中:
Map<String,IGameObject> inventory = new HashMap<String,IGameObject>();
当我检索它时,我会得到一个IGameObject
,如何正确转换它,以便我可以访问ChargeGun
中的方法?
您可以使用 visitor pattern 让您免于投射。这个想法很简单:你有一个 IGameObject 清单,它有一个直接调用 v.visit(this) 的方法 accept(GameObjectVisitor v)。在您的 GameObjectVisitor 中,您只需为每个实现实现访问方法:例如访问(Chargegun g)、访问(Sword s)等...
换个说法,就像回旋镖的原理:GameObjectVisitor调用item.accept(this),Item实现accept(GameObjectVisitor g),简单的g.visit(this ).
通过这样做,Visitor 对每个实现都有多个访问方法,并且可以执行特定的操作而无需 cast/using instanceof。