Java 如何强制转换子类实例

Java 如何强制转换子类实例,java,casting,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.entit

我真的不知道该怎么说,但基本上我需要得到一个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
    }

}
但假设我有多个“演员”。如果我不需要的话,我不想投


对不起,如果我没有很好地解释这一点。

我不知道你在问什么-所以我想你可能想做的是使用玩家作为演员,对吗?通过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; Actor temp=new Actor(){//实现抽象方法(如果有)} 演员玩家=新玩家()//这仍然很好,因为演员是玩家和演员共同的超级角色 玩家另一个=(玩家)玩家;//打字之后就可以了 ////但是 另一个=玩家;//编译错误,类型不匹配 另一个=(玩家)临时;//ClassCastException,但没有编译错误;
但是你仍然可以使用不同的演员和玩家作为演员。

你能解释一下你对“子实例”的理解吗?我如何根据ActorType向NPC或玩家适当地施放角色。NPC和玩家角色都包含该类型角色的唯一数据,所以我要问的是,如果我有一个攻击者和一个防御者,一个是玩家演员另一个是NPC演员我如何区分两者并获得合适的演员阵容?1)使用两个演员作为演员并将数据放入演员类中,或者您可以随时检查您的实例是什么类型的。此外,继承的全部要点是将常见的东西放入一个类中,将差异放入另一个类中这是从普通类继承而来的,所以我的建议是——走这条路,每个NPC玩家等都有一个superclass,并将所有这些实例作为superclass类型线程。 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;