【问题标题】:Difficulty accessing an ArrayList in another class in the package [closed]难以访问包中另一个类中的 ArrayList [关闭]
【发布时间】:2015-01-30 10:21:10
【问题描述】:

我的 Java 经验非常有限,我在访问同一包中的单独文件中的另一个类中的数组列表时遇到问题。

我在哪里声明数组似乎无关紧要..它不能从其他类访问

我的头等舱是这样的:

package 1st_class;

import java.awt.*;
import javax.swing.*;
import java.awt.Event.*;
import java.util.ArrayList;

public class 1st_class {


    public void main(String[] args) 
    {
        ArrayList<Test> tests = new ArrayList<Test>(); 
        ArrayList<Score> scores = new ArrayList<Score>();

        tests = new ArrayList<Test>(); 
        scores = new ArrayList<Score>();

        MainMenu Menu1 = new MainMenu();
        Menu1.setVisible(true);
    }
}

这似乎只能识别这段代码中的数组..我可以通过在另一个类中拥有一个 mainmenu 实例并在数组名称前面加上 main 来引用它们。

我对我猜的范围感到困惑。

【问题讨论】:

  • 它是 local 变量。即使在 same 类中的其他方法中也无法访问它。我强烈建议您通过 basic tutorials 了解该语言的基础知识。
  • 调用类1st_class合法吗?
  • @khelwood 这是“合法的”,但他应该检查命名约定:en.wikipedia.org/wiki/Naming_convention_(programming)#Java
  • @tumisma 标识符不能以数字开头。 1st_class 不是合法标识符。

标签: java object arraylist scope


【解决方案1】:

您的 ArrayList 变量是您的主要方法的本地变量。为了使其他方法/类可以访问它们,您应该将它们作为参数传递给这些方法,或者您应该在类级别声明它们。

顺便说一句,您的主要方法缺少static

public class 1st_class {

    private static ArrayList<Test> tests = new ArrayList<Test>(); 
    private static ArrayList<Score> scores = new ArrayList<Score>();

    public static void main(String[] args) 
    {

但是,在 OOP 中,将它们设为公共成员通常被认为是不好的做法。如果其他类需要访问它们,您应该将它们声明为私有并将公共方法添加到1st_class,这将使其他类可以访问它们。

【讨论】:

    【解决方案2】:

    如果您希望类中的变量对其他人可见,则它需要是公共类字段。现在它是一个块局部变量,仅在当前块中可见。

    尝试像这样定义它们:

    class Something {
        public ArrayList<Test> test = new ArrayList<Test>();
    }
    

    记住,如果你这样做,你不要然后在函数中使用它们,它们的类型在它们前面,或者你会用同名的局部变量覆盖它们。

    【讨论】:

      【解决方案3】:

      您的列表仅在您的主方法中可见,因此对其他人的外部主方法不可见。

      公开 ArrayList 的一种方法是通过访问修饰符,即公开访问您的两个列表,如下所示:

      public class StClass {//i have changed name to follow camel case. Give it meaningful name
          public static ArrayList<Test> tests = new ArrayList<Test>(); 
          public static ArrayList<Score> scores = new ArrayList<Score>();
      }
      

      另一种方法是公开 getter/setter 静态方法,以便您可以在类级别访问,如下所示。

      public static ArrayList<Test> getTestList() {
           return tests;
      }//and similarly for scores.
      

      然后从另一个包中调用它:

      StClass.tests
      

      ArrayList<Test> testList = StClass.getTestList();
      

      【讨论】:

        【解决方案4】:

        如果你想在另一个类中调用它,你应该尝试这样的事情。

        package 1st_class;
        
        import java.awt.*;
        import javax.swing.*;
        import java.awt.Event.*;
        import java.util.ArrayList;
        
        public class 1st_class {
        
            public ArrayList<Test> tests = new ArrayList<Test>(); 
            public ArrayList<Score> scores = new ArrayList<Score>();
        
            public void main(String[] args) 
            {    
                MainMenu Menu1 = new MainMenu();
                Menu1.setVisible(true);
            }
        }
        

        现在您可以通过以下方式在任何地方访问测试和分数:

        1st_class.tests
        1st_class.scores
        

        正如他们告诉你的,你应该创建 getter/setter。如果您创建 getter 和 setter,您可以将变量更改为私有。

        public ArrayList<Test> getTests() {
            return tests;
        }
        
        public void setTests(ArrayList<Test> tests) {
            this.tests = tests;
        }
        
        public ArrayList<Score> getScores() {
            return scores;
        }
        
        public void setScores(ArrayList<Score> scores) {
            this.scores = scores;
        }
        

        【讨论】:

          【解决方案5】:

          尝试在您需要的类中编写getArrayList() 方法或将其定义为static

          public class 1st_class {
                  public static ArrayList<Test> tests = new ArrayList<Test>(); 
                  public static ArrayList<Score> scores = new ArrayList<Score>();
          
              public void main(String[] args) 
              {
                  MainMenu Menu1 = new MainMenu();
                  Menu1.setVisible(true);
              }
          }
          
          public newclass {
          
              public newclass() {
                  1st_class .tests.add(new Test);
              }
          
          }
          

          或者你可以这样做:

           public class 1st_class {
                  private ArrayList<Test> tests = new ArrayList<Test>(); 
                  private ArrayList<Score> scores = new ArrayList<Score>();
          
              public void main(String[] args)  {
          
                  MainMenu Menu1 = new MainMenu();
                  Menu1.setVisible(true);
              }
          
              public ArrayList<Test> getArrayList() {
                   return tests;
              }
          }
          
          public newclass {
          
              public newclass() {
                  ArrayList<Test> temp = new 1st_class().getArrayList();
              }
          
          }
          

          【讨论】:

            【解决方案6】:

            是的,问题在于您要访问的变量的范围或可见性。要从其他类访问数组,您必须将它们创建为您的类的成员:

            public class YourClass {
            
                public ArrayList<Test> tests = null;
                public ArrayList<Scores> scores = null;
            
                // I guess you forgot "static" in here
                public static void main(String[] args) {
                    // you are in a static context, therefore you have to instantiate the class you want to access
                    YourClass myClass = new YourClass();
            
                    // now you can access the fields from your class
                    myClass.tests = new ArrayList<Test>(); 
                    myClass.scores = new ArrayList<Score>();
                }
            
            }
            

            您也可以从同一包中的另一个类访问这些字段:

            public class YourOtherClass {
                public void yourMethod() {
                    YourClass myClass = new YourClass();
                    myClass.tests.add(new Test());
            
                    // should be 1
                    System.out.println("The list has the following size:" + myClass.tests.size());
                }
            }
            

            使字段可直接访问通常是一种不好的做法。你可能想看看encapsulation in Java

            亲切的问候

            弗洛里安

            【讨论】:

              猜你喜欢
              • 2014-12-17
              • 2013-07-18
              • 1970-01-01
              • 1970-01-01
              • 2015-05-31
              • 1970-01-01
              • 2021-01-27
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多