gpt4 book ai didi

java - 检测实现接口(interface)的对象的类

转载 作者:行者123 更新时间:2023-11-30 02:34:40 24 4
gpt4 key购买 nike

我正在编写一个游戏,作为该游戏的一部分,玩家应该能够单击各种 GUI 项目并查看 GUI 特定区域的更多详细信息。我正在通过界面 Detailable 来处理这个问题它由适当的游戏对象实现并将适当的信息发送到 JPanel

还有一些容器(所有容器都实现 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);
}

最佳答案

您实际上可以使用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
}

关于java - 检测实现接口(interface)的对象的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43420421/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com