[英]One array containing different Object classes
所以我有一個Object類Item
和一個Object類Trap
。 這兩個變量共享名為name
和icon
變量。 我希望能夠將這兩個類放入一個稱為special
數組中,並能夠通過使用special
訪問Item
和Trap
的組件。 這是我需要工作的代碼。
if (special[x][y] == null)
return 0;
System.out.print(special[x][y].icon); /* here's where the issue is */
return 1;
什么是special
的數組? 我應該使用界面嗎? 如果是這樣,怎么辦? 我到處都在閱讀,但是我不知道如何描述我的問題。
試試看:
class Base
{
String name;
Icon icon;
}
class Item extends Base { }
class Trap extends Base { }
List<Base> special = new ArrayList<>();
或者,可以將Base
為接口,並使用implements
關鍵字,而不是Item
和Trap
的extends
。
一種可行的方法是使用超類(類,抽象類,接口都可以工作)。 對於超類來說,這是一個不好的名字,但是我相信您會明白的:
public class Thing {
String icon; // Every class that extends Thing has an icon
String name; // Every class that extends Thing has a name
public Thing(String newIcon, String newName) {
icon = newIcon;
name = newName;
}
public String getIcon() {
return this.icon;
}
public String getName() {
return this.name;
}
}
public class Trap extends Thing {
public Trap() {
super("newIcon", "newName"); // Sets this Traps's name and icon values
}
}
public class Item extends Thing {
public Item() {
super("newIcon", "newName"); // Sets this Item's name and icon values
}
}
您可以添加任何想要的陷阱和項的方法/變量,並且只要它們本身合法,它們就可以工作。
為Item
和Trap
一個接口,其中包含它們應共享的方法。
public interface GameObject {
String getName();
Image getIcon();
}
然后,您可以通過實現此接口來創建Item
和Trap
類。 例如
public class Trap implements GameObject {
private String name;
private Image icon;
public GameObject(String name, Image icon) {
this.name = name;
this.icon = icon;
} ...
通過聲明此類實現了GameObject
這意味着我們必須創建getName
和getIcon
方法。 您可以通過使用@Override
注釋來實現。
public class Trap implements GameObject {
private String name;
private Image icon;
public GameObject(String name, Image icon) {
this.name = name;
this.icon = icon;
}
@Override
public String getName() {
return name;
}
@Override
public Image getIcon() {
return icon;
}
}
現在它實現了GameObject
我們可以將其添加到保存GameObject
類型的列表中
List<GameObject> special = new ArrayList<>();
myList.add(new Trap(myTrapName, myTrapImage));
myList.add(new Item(myItemName, myItemImage));
然后,我們可以調用方法而不必擔心特定的GameObject
是Item
還是Trap
for (GameObject obj : special) {
System.out.println(obj.getName());
}
您應該使用接口
public interface SpecialInterface{
//you actually do not need any code, usually interfaces are called something+able
String getIcon();//this method will be filled in all the objects implementing this interface
}
因此,現在您可以在兩個類中都實現該接口,例如:
public class Trap implements SpecialInterface{
//...
}
現在您要迭代項目和陷阱,可以執行以下操作:
System.out.println(special[x][y].getIcon());
要么..
if(special[x][y] instanceof Trap){
Trap oneTrap = (Trap) special[x][y]; //here you transform your SpecialInterface object in a Trap object
System.out.println(special[x][y].icon);
}else{
Item oneItem = (Item) special[x][y];
System.out.println(special[x][y].icon);
}
注意:Trap和Item對象應聲明為:
SpecialInterface trap = new Trap();
要么
SpecialInterface item = new Item();
否則,您將無法將此對象插入矩陣。
什么是特殊的數組? 我應該使用界面嗎? 如果是這樣,怎么辦?
如果想要一個可以容納兩種不同類型元素的數組,則其元素類型必須是兩者的超類型。 那可能是兩種類型都實現的接口,也可能是兩者的超類。
我鼓勵您通過訪問器方法(例如getIcon()
)訪問成員。 如果你堅持直接訪問他們,在你的榜樣,那么接口選項是不可能的,你要訪問的成員必須屬於超類(或它的一個父)。
例如,
public interface GameObject {
String getName();
Icon getIcon();
}
public class Trap implements GameObject {
private final String name;
private final Icon icon;
public Trap(String name, Icon icon) {
this.name = name;
this.icon = icon;
}
@Override
public String getName() {
return name;
}
@Override
public Icon getIcon() {
return icon;
}
}
(...和Item
...類似)
GameObject[][] special = /* ... */;
// ...
if (special[x][y] == null) {
return 0;
}
System.out.print(special[x][y].getName());
return 1;
public class Entity {
String name;
Icon icon;
}
public class Item extends Entity {
...
...
}
public class Trap extends Entity {
...
...
}
Entity[][] special = new Entity[5][10];
現在您可以在special
2D數組中插入兩個類之一,並且可以使用問題中提供的完全相同的代碼。
if (special[x][y] == null)
return 0;
System.out.print(special[x][y].icon); /* here's where the issue is */
return 1;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.