我正在编写一个游戏,因为该玩家的一部分应该能够点击各种GUI项目并查看GUI特定区域的更多细节。我通过由合适的游戏obects实现并发送相应的信息到JPanel
检测实现接口的对象的类
接口Detailable
mangaing这也有容器(全部实现Detailable
)包含其他(Detailable
实施)对象。作为它的目标是可以点击的容器上,其中包括它的统计数据,查看其内容,然后可以依次点击查看他们统计等
我有在写的问题addToContents(Detailable d)
我的容器的方法。每个容器作为容器的“类型”的一个ArrayList<String>
- 衣柜,书柜等。我希望能够仅将某些类添加到给定容器 - 因此具有“书柜”类型的容器将只接受类的对象例如,Book
或Curio
。
我目前拥有的是:
public boolean addToContents(Detailable d){
if(this.types.contains("bookcase") && d.getClass().getName().equals("Book")){
//do some stuff
//I know "Book" isn't the right syntax, this is just to demo
return true;
}
else if(this.types.contains("bookcase") && d.getClass().getName().equals("Curio")){
//other stuff
return true;
}
//etc
else{
return false;
}
}
但是,这感觉就像做的错误的方式。有没有更好的办法?理想情况下,为方便起见,代码,我不得不像(伪)
Constructor:
private ArrayList<Class> classesAccepted = <list of classes>
addToContents:
if (classesAccepted.contains(d.getClass()){
add the thingie to contents
return true
}
else{
return false;
}
,但我似乎无法找到添加类的列表来构造的一种方式 - 翻译类的ArrayList名称引用到实际类的引用的ArrayList。
集装箱正在从一个JSON看了这么包括两类:
public class FurnitureType {
private String name;
private List<String> type;
private int cost;
private String description;
private int comfortBonus;
private int capacity;
//plus getters for all the above
}
public class Furniture implements Detailable, ListSelectionListener{
private String name;
private List<String> types;
private int cost;
private String description;
private int comfortBonus;
private int capacity;
private ArrayList<Detailable> contents;
private transient DetailPanel dp = null;
public Furniture (FurnitureType type){
this.name=type.getName();
this.types = type.getType();
this.cost = type.getCost();
this.description = type.getDescription();
this.comfortBonus = type.getComfortBonus();
this.capacity = type.getCapacity();
this.contents = new ArrayList();
}
//appropriate getters
public boolean addToContents(Detailable d){
if(this.types.contains("bookcase") && d.getClass().getName().equals("Book")){
//do some stuff
//I know "Book" isn't the right syntax, this is just to demo
return true;
}
else if(this.types.contains("bookcase") && d.getClass().getName().equals("Curio")){
//other stuff
return true;
}
//etc
else{
return false;
}
}
@Override
public String toString(){
return description;
}
@Override
public Icon getBigPic() {
return null;
}
@Override
public JComponent getStats() {
Object [] objectContents = contents.toArray();
JList contentList = new JList(objectContents);
contentList.setPreferredSize(new Dimension (400, 300));
contentList.setFixedCellHeight(50);
contentList.addListSelectionListener(this);
contentList.setCellRenderer(new CustomCellRenderer());
//the CustomCellRenderer class simply makes long descriptions into multiline cells
return contentList;
}
@Override
public void addPanel(DetailPanel dp) {
this.dp = dp;
}
@Override
public void valueChanged(ListSelectionEvent lse) {
Detailable d = contents.get(lse.getFirstIndex());
dp.updatePanel(d);
}
你几乎有你的伪代码吗?问题是什么? – developer
对不起,将更新问题的清晰度 - 你是对的我还没有特别明确地提出这个问题 – MrB
书和古玩有一个共同的超类型? – developer