English 中文(简体)
哪些是更好的 Java方案拟订做法:打 en和 en,或分类?
原标题:Which is better Java programming practice: stacking enums and enum constructors, or subclassing?

鉴于某些种类不同的物品数量有限,代表这些物品的 st头和 en子,或将其分类更好吗? 还是完全有更好的办法?

为了给你某种背景,在我的小型RPG方案(具有讽刺意味的是简单)中,特性在其库存中具有不同种类的物品。 项目类型、用途和效果各不相同。

例如,一个存货项目是称为Gremlin的通量,调整了Utility属性。 另一件物品可能是一把 sword剑,用于战斗并造成破坏。

在我的RPG代码中,我现在尝试了两种方式来代表库存项目。 一种方式是分级(例如,盘存系统->Spell-> AdjustingAttributes;盘存)。 项目---> 武器-> Sword)和在需要时调和每一次子类,并分配如格雷姆林和莫尔特等值。

另一种方式是打 st和 en。 例如,我为项目目录和项目名称及武器表设定了标记,而盘存表也一样:

public enum InventoryItem {
   GREMLIN(itemType.SPELL, itemSpellTypes.ATTRIBUTE, Attribute.UTILITY),
   MORT(itemType.WEAPON, itemWeaponTypes.SWORD, 30);

   InventoryItem(itemType typeOfItem, itemSpellTypes spellType, Attribute attAdjusted) {
   // snip, enum logic here
   }
   InventoryItem(itemType typeOfItem, itemWeaponTypes weaponType, int dmg) {
   // snip, enum logic here 
   }
   // and so on, for all the permutations of items. 

}

Java的方案拟订做法是否比这两种方法好? 或者这些是唯一的途径,其中哪些是更好的? 事先感谢您的建议。

最佳回答

在你描述的情况下,我会考虑采用等级制而不是定义,并以接口补充这一等级。

/**
 * Root of class hierarchy.
 */
public interface InventoryItem {
}

/**
 * Additional "parallel" interface implemented by some (but not all)
 * InventoryItems and other non-inventory items.
 */
public interface Usable {
  void use();
}

/**
 * A Spell is in InventoryItem and is also Usable.
 */
public abstract class Spell implements InventoryItem, Usable {
}

public class Gremlin extends Spell {
}

/**
 * A Door is *not* an InventoryItem but can be used.
 */
public class Door implements Usable {
}

这种做法的主要好处是,它允许你在different context(Inventory>)上处理某一物体。 项目或作为<编码>的一览表 我偏离表的另一个原因是,你可能重新界定贵重物品的行为(例如:spell.cast(Person)),而这在IMHO大体中是一帆风顺的。

问题回答

如果只有静态数据,而且逻辑性非常低(最好是完全没有逻辑)。 如果贵物体的用途取决于其类型而有不同的行为,那么作为子类加以实施通常是一个好的想法。

正如其他人提到的那样,你很可能对你的物品添加属性,这些属性在各种方面都产生了意义。 例如,对武器而言,其含义是“肤色”的。 在这种情况下,武器与拼凑不应相同。

如果你想给这些物品增加新的属性,那么作为子类执行这些特性会比较容易。 这样,如果一组物品得到新的特性,就会向所有子类传播。

例如,如果所有类型的<武器编码>>都带有一定的权重,那么你只是补充这一类。 之后,这一特性将传播给子类。

关于其他更静态、因而可以直接分配的事物,情况良好。 我在这里自由思考,因此我猜测一个<条码> 武器/编码>。 分配给它:

public abstract class Weapon {

public enum DamageType {

 CUT,
 SMASH,
 PIERCE;
}

private DamageType damageType;




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...