Java 如何在一个类中构造多个对象,然后在另一个类中读取
如果这是一个基本问题,我很抱歉 我试图在一个类中创建几个唯一的对象,然后在另一个类中获取其中一个对象的值 我创建了两个类,并通过一些示例来结束本文Java 如何在一个类中构造多个对象,然后在另一个类中读取,java,class,object,variables,Java,Class,Object,Variables,如果这是一个基本问题,我很抱歉 我试图在一个类中创建几个唯一的对象,然后在另一个类中获取其中一个对象的值 我创建了两个类,并通过一些示例来结束本文 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 advan
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类中,fire“无法解析或不是字段”
编辑以提供更清晰的目的。您在这里完全混合了东西 在类型的concstructor中,您正在创建一个名为fire的新本地对象。此对象仅在此构造函数中可用,不在其外部,例如在主类中
只有在您提供有关您试图实现的目标的更多信息时,才能找到有效的解决方案。我可以这样写您的类型结构:
public Type() {
this.name = "Fire";
this.healthmod = 0;
this.strmod = 1;
this.accmod = 0;
this.armmod = 0;
this.refmod = 0;
this.intmod = 1;
}
因此,在主方法中使用Player类时,如下所示:
public static void main(String args[]) {
Type type = new Type();
Player.typename = type.name;
}
public class Player {
public static Type fire;
}
public static void main(String args[]) {
Player.fire = new Type();
System.out.println(Player.fire.name);
}
您还可以在Player类中放置类型为的引用,如下所示:
public static void main(String args[]) {
Type type = new Type();
Player.typename = type.name;
}
public class Player {
public static Type fire;
}
public static void main(String args[]) {
Player.fire = new Type();
System.out.println(Player.fire.name);
}
因此,在您的主要方法中:
public static void main(String args[]) {
Type type = new Type();
Player.typename = type.name;
}
public class Player {
public static Type fire;
}
public static void main(String args[]) {
Player.fire = new Type();
System.out.println(Player.fire.name);
}
我试图完成的是创建几个对象,然后使用主类选择其中一个对象,并将值传递给Player类。如果这是一个非常基本的问题,我再次道歉。十多年后,我又开始学习Java。你想有一些常量可以在以后使用吗?你能编辑以提供你的播放器类吗?用播放器类编辑吗