2017-04-14 54 views
1

我正在編寫一個遊戲,因爲該玩家的一部分應該能夠點擊各種GUI項目並查看GUI特定區域的更多細節。我通過由合適的遊戲obects實現併發送相應的信息到JPanel檢測實現接口的對象的類

接口Detailable mangaing這也有容器(全部實現Detailable)包含其他(Detailable實施)對象。作爲它的目標是可以點擊的容器上,其中包括它的統計數據,查看其內容,然後可以依次點擊查看他們統計等

我有在寫的問題addToContents(Detailable d)我的容器的方法。每個容器作爲容器的「類型」的一個ArrayList<String> - 衣櫃,書櫃等。我希望能夠僅將某些類添加到給定容器 - 因此具有「書櫃」類型的容器將只接受類的對象例如,BookCurio

我目前擁有的是:

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); 
} 
+2

你幾乎有你的僞代碼嗎?問題是什麼? – developer

+0

對不起,將更新問題的清晰度 - 你是對的我還沒有特別明確地提出這個問題 – MrB

+0

書和古玩有一個共同的超類型? – developer

回答

0

如果我正確理解你的問題,您正在尋找這樣的事情

ArrayList <Class<? extends Detailable>> acceptedClasses = new ArrayList<>(); 

acceptedClasses.add(Bookcase.class); 
acceptedClasses.add(OtherAcceptable.class); 

,然後做類似於

boolean test = 
    acceptedClasses.stream().anyMatch(clazz -> aClass.isInstance(detailableInstance)); 

檢查,如果情況是可以接受的類型

+0

只需'新ArrayList <>()'應該罰款在Java 7+ –

+0

尼斯,謝謝。 – MrB

+0

這段代碼不能編譯 –

2

實際上,你可以使用一個Map爲如下圖所示:

private static Map<String, List<Class<? extends Detailable>>> 
     bookcaseContainer = new HashMap<>(); 

static { 
     //load the bookcaseContainer Map from properties/database 
     bookcaseContainer.put("bookcase", list1); 
     bookcaseContainer.put("wardrobe", list2); 
    } 


if(bookcaseContainer.get("bookcase") != null && 
     bookcaseContainer.get("bookcase").contains(d.getClass())) { 
     //do something here 
} else if(bookcaseContainer.get("wardrobe") != null && 
     bookcaseContainer.get("wardrobe").contains(d.getClass())) { 
     //do something here 
} 
+0

很好的答案,並感謝所有的幫助。接受另一個代碼稍短,我不能接受兩個,但它是一個很好的解決方案。 – MrB