我正在編寫一個遊戲,因爲該玩家的一部分應該能夠點擊各種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