2017-04-14 64 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