私はこのゲームの一部として、さまざまなGUIアイテムをクリックしてGUIの特定の領域に関する詳細を見ることができるように、ゲームを作成しています。私は(Detailable
が実装する)他のオブジェクトを含むコンテナが(Detailable
を実装するすべてが)もあり、適切なゲームobectsによって実装されJPanel
インタフェースを実装するオブジェクトのクラスを検出する
に適切な情報を送信しているインタフェースDetailable
を介してこれをmangaingよ。目標はコンテナをクリックし、その統計情報の中でその内容を確認し、その内容をクリックしてのの統計情報などを見ることができます。
私が抱えている問題は、 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に名前を追加します。私が正しくあなたの質問を理解していれば、あなたはこの
ArrayList <Class<? extends Detailable>> acceptedClasses = new ArrayList<>();
acceptedClasses.add(Bookcase.class);
acceptedClasses.add(OtherAcceptable.class);
のようなものを探して、次にやるさ
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
BookとCurioに共通のスーパータイプがありますか? – developer