關于堆疊溢位的第一個問題!我覺得這已經是Java的當中一個初學者常見的問題。但是我已經嘗試了幾個小時并且無法找到解決方案。我以為那個物件的屬性可以通過這種方式來訪問。
起初我以為那weapon[0]實際上是一個物件陣列,所以當我創建物件陣列時Inventory[] inventory,我在建構式中使用了一個物件陣列。我立即解決了這個問題,但這個問題仍然存在。
更煩人的是,在除錯模式下,我可以從字面上看到 其屬性的weapon[0]內部Inventory[] inventory。
看 Eclipse 嘲笑我。
我目前的理論是,在物件陣列中放置 object weapon[0], class 的實體可能是問題所在,并且由于該放置,無法以某種方式訪問??物件的屬性。任何幫助將不勝感激,謝謝!這是我第一次搞亂陣列,所以我絕對是個新手。關于我的格式等的任何提示也將非常有幫助!WeaponsInventory[] inventory
package arraytest;
import java.util.Scanner;
import java.util.InputMismatchException;
import java.lang.NumberFormatException;
public class ArrayTest {
static Scanner kb = new Scanner(System.in);
static int i = 0;
static int choice = 0;
public static void main(String[] args) {
Weapons[] weapon = new Weapons[3];
weapon[0] = new Weapons(0,"Wooden Sword",1,2);
weapon[1] = new Weapons(1,"Bronze Sword",2.5,7.5);
weapon[2] = new Weapons(2,"Iron Sword",5,10);
Armor[] armor = new Armor[3];
armor[0] = new Armor(3,"Wooden Armor",2,5);
armor[1] = new Armor(4,"Bronze Armor",3,10);
armor[2] = new Armor(5,"Iron Armor",5,15);
Enemy[] enemy = new Enemy[3];
enemy[0] = new Enemy(0,"Skeleton",3,0,10);
enemy[1] = new Enemy(1,"Goblin",2,1,5);
enemy[2] = new Enemy(2,"Zombie",4,1,8);
Inventory[] inventory = new Inventory[256];
String chooseweapon = String.format(
"Choose your weapon:\n"
"1. %s\n"
"2. %s\n"
"3. %s\n"
,
weapon[0].name,
weapon[1].name,
weapon[2].name
);
System.out.print(chooseweapon);
while (i==0) {
i ; //1
try {
choice = Integer.parseInt(kb.nextLine());
} catch (NumberFormatException e) {
System.out.println("Error. Try again.");
i--; //0
continue;
}
if (choice < 1 || choice > 3) {
System.out.println("Error. Try again.");
i--; //0
}
}
if (choice == 1) {
inventory[0] = new Inventory(weapon[0]);
}
System.out.println(inventory[0].item); //this is the problem here. i can't put .item.name, error is "name cannot be resolved or is not a field"
}
}
class Inventory {
public Object item;
Inventory(Object item) {
this.item = item;
}
}
class Armor {
public int id;
public String name;
public double defnum;
public double val;
Armor(int id, String name, double defnum, double val) {
this.id = id;
this.name = name;
this.defnum = defnum;
this.val = val;
}
}
class Weapons {
public int id;
public String name;
public double attdmg;
public double val;
Weapons(int id, String name, double attdmg, double val) {
this.id = id;
this.name = name;
this.attdmg = attdmg;
this.val = val;
}
}
class Enemy {
public int id;
public String name;
public double attdmg;
public double defnum;
public double health;
Enemy(int id, String name, double attdmg, double defnum, double health) {
this.id = id;
this.name = name;
this.attdmg = attdmg;
this.defnum = defnum;
this.health = health;
}
}
uj5u.com熱心網友回復:
歡迎使用 StackOverflow!這是一個措辭很好的問題!
這與運行時型別與編譯時型別之間的差異有關。您宣告item為 型別Object。
Java 允許多型性,當您將itemin宣告Inventory為 type 時Object,它允許您將任何item“is-a”Object分配給任何物件(這意味著您可以將 a String、 an Integer、任何物件分配給item,因為這些都繼承自Object類)。
然而,當你去訪問item后來在節目中,爪哇不能保證在編譯時,無論item參考擁有name的財產!Integer,例如,是 Object的,但不具有name財產!Java 編譯器只是說,“我只知道這item是一個Object,我不會讓您訪問并非所有Objects 都擁有的屬性!”。
但是,當您運行該程式時,運行時型別item是Weapon,因此 Eclipse 能夠向您顯示其屬性。但是 Java 被設計為在編譯時捕獲盡可能多的錯誤,因此name如果它不能在編譯時保證它具有名稱,它將不允許您訪問該屬性。
這可能看起來很煩人或有不必要的限制(你知道你要放入的所有東西Inventory都會有一個名字!),所以這就是超類和介面的重點!這些特性使您可以靈活地創建共享相似屬性或方法的不同型別的物件,并且仍然允許 Java 預先捕獲所有這些潛在問題。
要解決此問題,您可以創建一個具有屬性的和擴展的InventoryItem超類。然后您可以宣告為 type 。這樣,Java 就會知道,即使運行時型別可能是 a或,它也保證有一個名稱。ArmorWeaponnameitemInventoryItemWeaponArmor
我們可以引入一個新類,例如InventoryItem:
class Inventory {
public InventoryItem item;
public int id;
Inventory(InventoryItem item, int id) {
this.item = item;
this.id = id;
}
}
然后班級Inventory可以接受InventoryItem(我建議可能包含物品陣列的庫存)
class Inventory {
public InventoryItem item;
Inventory(InventoryItem item) {
this.item = item;
}
然后您的類,例如Armor,可以擴展InventoryItem,例如:
class Armor extends InventoryItem {
public double defnum;
public double val;
Armor(int id, String name, double defnum, double val) {
super(name, id);
this.defnum = defnum;
this.val = val;
}
}
然后這將起作用!
System.out.println(inventory[0].item.name); //Java now knows that inventory[0] is type Inventory, its "item" property is type InventoryItem, and that is *guaranteed* to have a name and id!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/342695.html
上一篇:用字典陣列過濾字典陣列
