【问题标题】:Java | acessing object variables from inside constructor?爪哇 |从构造函数内部访问对象变量?
【发布时间】:2019-02-20 23:44:27
【问题描述】:

我通常不使用 Java,我目前正在尝试帮助朋友完成 Java 作业,这让我陷入困境

我正在尝试访问我在 Object 的构造函数中创建的数组,但我不知道如何访问它。

public class ADTbag {
   String item = "Testing";


   public ADTbag(int size) {
      // This constructor has one parameter, name.
      String[] bag = new String[size];

      bag[0] = Integer.toString(size);
      System.out.println("A bag was created with the size of " + size + " | " + bag[0]);
   }

   public void insert() {
      /* Insert an item */
      /* One Problem this public void doesn't have access to the bag var"
      System.out.println(bag);

   }

我觉得这是 java 中的一个简单概念,但我在谷歌上找不到任何对我有帮助的东西。我希望能够使用 insert 方法在包或字符串数​​组对象中插入一些东西。所以像这样。

public static void main(String []args) {
      /* Object creation */
      ADTbag myBag = new ADTbag(5);

      String value = "Some Value";
      /* I want to do this */
      mybag.insert(value);


   }
}

【问题讨论】:

  • 你和你的朋友需要了解classlocal变量的区别。

标签: java arrays object constructor


【解决方案1】:

您需要将bag 设为类成员,以便可以在构造函数之外访问它。

【讨论】:

    【解决方案2】:

    将变量定义为实例变量

    public class ADTbag {
           String item = "Testing";
           String[] bag;
    
           public ADTbag(int size) {
              // This constructor has one parameter, name.
             this.bag = new String[size];
    
              bag[0] = Integer.toString(size);
              System.okaut.println("A bag was created with the size of " + size + " | " + bag[0]);
           }
    
           public void insert() {
              /* Insert an item */
              /* One Problem this public void doesn't have access to the bag var"
              System.out.println(bag);*/
    
           }
    }
    

    如下所示。

    【讨论】:

      【解决方案3】:

      首先,您必须使 bag 字段具有全局性。之后,我们可以创建一个函数来向您的包中插入/添加新元素。然后没有必要使用构造函数,就像您尝试做的那样。

      另一件事是,当您在谈论向“列表”插入和/或添加项目时,使用 ArrayList 而不是标准的 array 就足够了。

      ArrayList 是一个数据/集合结构,使您能够在运行时在同一对象上方添加、删除、设置、获取(和一些其他操作)。如果我们想在数组中插入一个新项目,我们不能;为此,我们必须创建另一个 size+1 的数组,然后设置新数组的所有元素。那么这对于一个简单的操作来说是如此令人困惑。

      想到这里我会给你一个使用这个的方法,看看:

      import java.util.ArrayList;
      
      public class ADTbag {
          /*
          global field to be referenced through entire class.
          We have to specify the type of objects that will be inserted
          inside this list, in this case String
           */
          ArrayList<String> bag;
      
          String item = "Testing";
      
          //constructor doesn't need parameter
          public ADTbag() {
              //here we init the bag list
              bag = new ArrayList();
      
              //adds your "standard item" on creating
              bag.add(item);
      
      
      
            /*
              prints your msg.
      
              - to get the size of a ArrayList just call list.size();
              - to get the item from the X index just call list.get(X)
               */
              System.out.println("A bag was created with the size of " + bag.size() + " | " + bag.get(0));
          }
      
          /*
          you doesn't need a new method
           */
      }
      

      要这样做:

      public static void main(String[] args) {
          ADTbag myBag = new ADTbag();
          myBag.bag.add("some value");
      }
      

      【讨论】: