可以使卡或者一个接口或抽象类。如果你使用后者,你实际上可以定义类似你上面已经有的逻辑。根据你走的路线,每个“卡”将实现接口或扩展抽象类。
接口的缺点是必须将所有字段重新实现为子类的专用变量。
摘要
public abstract class Card {
protected int power;
protected int value;
protected String type;
protected String cardName;
protected ImageIcon image;
// You cannot instantiate this directly, only through children.
public Card(int power, int value, String type, String cardName, ImageIcon image) {
this.power = power;
this.value = value;
this.type = type;
this.cardName = cardName;
this.image = image;
}
// Getters/Setters...
}
public class AttackCard extends Card {
public AttackCard(int power, int value, String type, String cardName, ImageIcon image);
super(power, value, type, cardName, image);
}
}
接口
public interface Card {
int getPower();
int getValue();
String getType();
String getCardName();
ImageIcon getImage();
}
public class AttackCard implements Card {
private int power;
private int value;
private String type;
private String cardName;
private ImageIcon image;
public AttackCard(int power, int value, String type, String cardName, ImageIcon image);
this.power = power;
this.value = value;
this.type = type;
this.cardName = cardName;
this.image = image;
}
// Implement methods from Card...
}
编辑
如果要实现日e DC漫画甲板游戏,那么你可能只需要为所有卡片提供1个抽象类,并为Hero,Villain,Super Power和Starter提供一个界面。不知道他们是什么类型的......然后所有的英雄会扩展抽象牌类以获得牌的所有共同属性,并实现特定于英雄的方法。
你不需要为每个卡片单独的类。每个英雄都只是英雄的一个实例。你只需要一个英雄卡集合。
// Hierarchy
abstract class Card
interface Hero
interface Villain
class HeroCard extends Card implements Hero
class VillainCard extends Card implements Villain
// Example constructors definitions for reference?
Card(String name, int value, int power);
HeroCard(String name, int value, int power);
VillainCard(String name, int value, int power);
// Create all heroes
List<Hero> heroes = new ArrayList<Hero>();
heroes.add(new HeroCard("Green Arrow", 5, 2));
// Create all villains
List<Villain> villains = new ArrayList<Villains>();
villains.add(new VillainCard("Ra's Al Ghul", 8, 3));
// All all the heroes and villains to all cards
List<Card> cards = new ArrayList<Card>();
cards.addAll(heroes);
card.addAll(villains);
所以你正在实施一个甲板建设游戏,是吧? –
哈哈是先生。这是DC甲板建设游戏,我爱上了它,并认为规则似乎对我的第一个真正的项目来说是如此僵化和完美的数字版本。 – lfernandes
[This one?](http://boardgamegeek.com/boardgame/125678/dc-comics-deck-building-game) –