【问题标题】:Strange runtime error declaring instance of class Java奇怪的运行时错误声明类 Java 的实例
【发布时间】:2014-09-08 18:29:06
【问题描述】:

我正在用 java 制作游戏,并且始终遇到最奇怪的错误。我有一门课叫武器。然后我创建了一个名为primary的实例。在我创建一个实例并将其称为辅助实例之后。出于某种奇怪的原因,primary 被secondary 的值覆盖。我和我的导师都看着它,无法弄清楚。代码如下:

public class weapon {
        static String type;
        static String name;
        static int weight;
        static int damage;
        static int dodge;
        weapon(String c, String n, int w, int da, int dod) {

                type = c;
                name = n;
                weight = w;
                damage = da;
                dodge = dod;

        }
        //getters
        String getType(){
                return type;
        }
        String getName(){
                return name;
        }
        Integer getWeight(){
                return weight;
        }
        Integer getDamage(){
                return damage;
        }
        Integer getDodge(){
                return dodge;
        }
        //setters
        void setType(String c){
                c=type;
        }
        void setName(String n){
                n=name;
        }
        void setWeight(Integer w){
                w=weight;
        }
        void setDamage(Integer da){
                damage=da;
        }
        void setDodge(Integer dod){
                dodge=dod;
        }
}

/*At the top of my main class I create both instances like this because the instances are created in if statements and I need to access them.*/
weapon primary;
weapon secondary;
//I create primary like this earlier in the code like this
primary = new weapon("primary","sword", 8, 6, -1);
//and then when I run this I get the output "sword" "Heavy Sword".
System.out.println(primary.getName());
secondary = new weapon("secondary", "huge sword", 9, 7, -2);
System.out.println(primary.getName());

【问题讨论】:

  • 请在此处分享相关代码。

标签: java class instance


【解决方案1】:

所有成员变量都定义为静态:

    static String type;
    static String name;
    static int weight;
    static int damage;
    static int dodge;

这就是为什么第二个实例的值会覆盖第一个实例的原因(因为静态成员是类可验证的 - 在类的所有实例中都有一个它们的副本)。

删除 static 关键字可以解决问题。

【讨论】:

    【解决方案2】:

    Weapon 类的所有属性都是静态的,这意味着它们在您创建的所有实例之间共享。

    删除 static 以使其成为实例变量,您应该没问题。

    【讨论】:

      【解决方案3】:

      您创建了一个具有类范围变量的类,而不是每个创建的对象都不同的变量。

      改为使用:

      public class weapon {
              private String type;
              private String name;
              private int weight;
              private int damage;
              private int dodge;
              weapon(String c, String n, int w, int da, int dod) {
      

      我建议您在定义类时使用以下模式,以帮助确保您的“类字段”和“对象字段”得到很好的描述

      public class <name-of-class> {
      // Class fields
          <private|public|protected> [final] static ....
      // Object fields
           private ...
      

      【讨论】:

        【解决方案4】:

        所有成员变量都声明为static。当您将成员变量声明为static 时,该类的所有对象共享这些变量的相同副本。如果一个对象改变了一个变量的值,它也会改变其他对象。

        只需删除 static 关键字。

        Weapon 似乎是一个 bean 类,如果使用 private 成员变量和 public getter/setter 正确封装,效果会更好。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-08-19
          • 1970-01-01
          • 2014-01-21
          • 1970-01-01
          • 2012-10-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多