これを正確に表現する方法はよくわかりませんが、基本的には、アクターの子クラスのインスタンスを割り当てずに取得する必要があります (それでよろしいですか?)。これは可能ですか?
package org.game.world.entity.actor;
import java.util.HashMap;
import java.util.Map;
import org.game.world.entity.Entity;
import org.game.world.entity.actor.npc.NPC;
import org.game.world.entity.actor.player.Player;
import org.game.world.entity.actor.player.PlayerData;
public abstract class Actor extends Entity {
/**
* The type of Actor this Entity should be
* recognized as.
*/
private final ActorType actorType;
/**
* A map of ActionStates, not necessarily 'Attributes'.
*/
private final Map<ActionState, Boolean> actionState = new HashMap<ActionState, Boolean>();
/**
* Constructs a new Actor {@Entity}.
*/
public Actor(ActorType actorType) {
this.actorType = actorType;
actionState.putAll(ActionState.DEFAULT_ACTION_STATES);
}
/**
* Gets the status of a {@Actor} ActionSate.
* @param state The ActionState.
* @return The ActionState flag.
*/
public boolean getActionState(ActionState state) {
return actionState.get(state);
}
/**
* Sets a {@Actor} ActionState flag.
* @param state The ActionState.
* @param flag The flag true:false.
*/
public void setActionState(ActionState state, boolean flag) {
actionState.put(state, flag);
}
/**
* Resets all ActionState's for this Actor.
*/
public void setDefaultActionStates() {
actionState.putAll(ActionState.DEFAULT_ACTION_STATES);
}
/**
* Checks if this Actor is a specific ActorType (i.e NPC)
* @param actorType The ActorType
* @return
*/
public boolean isActorType(ActorType actorType) {
return this.actorType == actorType;
}
/**
* The type of Actor.
*/
public static enum ActorType {
PLAYER,
NPC
}
}
アクター型。
package org.game.world.entity.actor.player;
import org.game.world.entity.Location;
import org.game.world.entity.actor.Actor;
import org.game.world.entity.actor.SkillLink;
/**
* This class represents a Player {@Actor} in the world.
*
* @author dillusion
*
*/
public class Player extends Actor {
/**
* This Player objects unique set of stored
* data.
*/
private final PlayerData playerData;
/**
* Creates a new Player object in the world.
* @param playerData The set of data unique to this Player.
*/
public Player(PlayerData playerData) {
super(ActorType.PLAYER);
this.playerData = playerData;
}
/**
* Gets the players name.
* @return The name.
*/
public String getName() {
return playerData.name;
}
/**
* Gets the players password.
* @return The password.
*/
public String getPassword() {
return playerData.password;
}
/**
* Gets the players permission level.
* @return The permission.
*/
public Permission getPermission() {
return playerData.permission;
}
/**
* Gets the players SkillLink instance.
* @return The SkillLink.
*/
public SkillLink getSkillLink() {
return playerData.skillLink;
}
@Override
public Location getLocation() {
return playerData.location;
}
@Override
public Location setLocation(Location location) {
return playerData.location = location;
}
}
しかし、複数の「アクター」があるとしましょう。必要がなければ、キャストする必要はありません。
これをうまく説明できなかったらすみません。