如何转换子类实例

时间:2013-08-08 17:56:36

标签: java casting

我真的不知道如何说出这个,但基本上我需要得到一个Actor的子类实例而不分配它(如果那样,那么?)。这可能吗?

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;
    }

}

但是,假设我有多个“演员”。如果我不需要,我不想要演员。

很抱歉,如果我没有很好地解释这一点。

1 个答案:

答案 0 :(得分:0)

我不知道你在这里有什么问题 - 所以我想你可能想要做的是使用Player作为演员吗?那么Java标准和继承是可能的

Actor temp=new Actor(){//implementing abstract methods if any}
Actor player=new Player(); //that is still fine as Actor is common superclas for player and actor

Player another=(Player)player; // thats just fine after typecasting
////but
another=player; // compile error, type mismatch 
another=(Player)temp; // ClassCastException but no compilation error;

但你仍然可以使用不同的演员和玩家作为演员。