-2

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

}

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

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

4

1 回答 1

0

我不知道你在这里问什么 - 所以我发现你可能想要做的是使用 Player 作为 Actor 对吗?这可以通过 Java 标准和继承来实现

Actor temp=new Actor(){//如果有的话,实现抽象方法}
演员 player=new Player(); //这仍然很好,因为 Actor 是玩家和演员的常见超类

另一个玩家=(玩家)玩家;// 类型转换后就好了
////但
另一个=玩家;// 编译错误,类型不匹配
另一个=(玩家)温度;// ClassCastException 但没有编译错误;

但是您仍然可以使用不同的 Actors 和 Players 作为 Actors。

于 2013-08-08T18:09:32.430 回答