2014-12-02 46 views
0

我的Java經驗非常有限,而且我在訪問另一個包中的另一個類中的數組列表時出現問題。在包中的另一個類中訪問ArrayList時遇到困難

這似乎並不重要,我聲明數組..它是不能從其他類訪問

我頂班是這樣的:

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。

我對我猜想的範圍感到困惑。

+3

這是** **局部變量。即使在* same *類的其他方法中也不可訪問。我強烈建議您閱讀[基礎教程](http://docs.oracle.com/javase/tutorial/)以瞭解該語言的基礎知識。 – Maroun 2014-12-02 08:27:22

+0

調用類'1st_class'是否合法? – khelwood 2014-12-02 08:30:34

+0

@khelwood它是「合法的」,但他應該檢查命名約定:http://en.wikipedia.org/wiki/Naming_convention_(programming)#Java – tumisma 2014-12-02 08:35:07

回答

0

你的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,這將使其他類可以訪問它們。

0

如果您希望某個類中的某個變量對其他人可見,則該變量需要是公共類字段。現在它是一個塊局部變量,它們只在當前塊中可見。

儘量將它們定義是這樣的:

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

請記住,如果你這樣做,你然後在他們面前使用它們內部功能與它們的類型,否則你會與當地的覆蓋它們變量名稱相同。

0

您的列表在您的主要方法中只是可見的,因此其他人的外部主要方法不可見。

一個揭露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 

OR

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

你應該嘗試這樣的事情,如果你想它調用另一個類。

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,則可以將這些變量更改爲private。

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; 
} 
2

嘗試寫在你需要的類getArrayList()方法或定義它像靜態

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(); 
    } 

} 
0

是的,問題是你要訪問的變量的作用域和可視性。爲了從其他類訪問數組,你必須創建爲您的類的成員:

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>(); 
    } 

} 

從另一個類在同一個包,你可以訪問這些字段,以及:

​​

製造領域直接訪問通常是一種不好的做法。你可能想看看encapsulation in Java

親切的問候

弗洛裏安