[英]How to construct several objects in one class, then read in another class
抱歉,這是一個基本問題。
我試圖在一個類中創建幾個唯一的對象,然后在另一個類中獲取一個對象的值。
我創建了兩個類,並遵循一些示例來結束
public class Type {
public String name;
public int healthmod;
public int strmod;
public int accmod;
public int armmod;
public int refmod;
public int intmod;
public String advantages;
public String disadvantages;
public Type() {
Type fire = new Type();
fire.name = "Fire";
fire.healthmod = 0;
fire.strmod = 1;
fire.accmod = 0;
fire.armmod = 0;
fire.refmod = 0;
fire.intmod = 1;
}
}
然后在主類中:
Player.typename = Type.fire.name;
編輯
public class Player {
public static String name, classname, racename, elementname;
public static int maxhealth, healthpts, healthptscost, healthupgnum, healthmod, currenthealth, basehealth;
public static int str, strpts, strptscost, strupgnum, strmod;
public static int acc, accpts, accptscost, accupgnum, accmod;
public static int arm, armpts, armptscost, armupgnum, armmod;
public static int ref, refpts, refptscost, refupgnum, refmod;
public static int intelligence, intpts, intptscost, intupgnum, intmod;
public static int mana, maxmana, managain, managainak;
public static int hitChance, critChance, Level, statPts, statTotal, damage, damageDealt, goldmult, itemmod, itemdefboost, itemdamboost, itemmodboost;
public static String[] focusStats;
}
我想做的是在Type類中創建一些對象並在Main類中訪問它們,以便將值存儲在Player類中,但是在Main類中,發生“無法解析或不是字段”
編輯以提供更清晰的目的。
您在這里完全混在一起。
在Type的構造函數中,您將創建一個名為fire的新本地對象。 該對象僅在此構造函數中可用,而在其外部(例如在主類中)不可用。
僅當您提供有關嘗試完成的更多信息時,才能找到有效的解決方案。
我可以這樣寫你的Type構造:
public Type() {
this.name = "Fire";
this.healthmod = 0;
this.strmod = 1;
this.accmod = 0;
this.armmod = 0;
this.refmod = 0;
this.intmod = 1;
}
因此,在Main方法中使用Player類時,如下所示:
public static void main(String args[]) {
Type type = new Type();
Player.typename = type.name;
}
您還可以像這樣在Player類中放置類型的引用:
public class Player {
public static Type fire;
}
所以在你這樣的主要方法中:
public static void main(String args[]) {
Player.fire = new Type();
System.out.println(Player.fire.name);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.