简体   繁体   English

如何使Main类中的变量可供其他类访问?

[英]How do I make variables in a Main accessible to other classes?

For example I have a MovieDatabase class that contains a list of Movie objects. 例如,我有一个MovieDatabase类,其中包含Movie对象的列表。 In my main code, I initialize all the objects in the MovieDatabase. 在我的主代码中,我初始化了MovieDatabase中的所有对象。 However I wish to call this MovieDatabase in another class to access the library. 但是,我希望在另一个类中调用此MovieDatabase来访问该库。 How would I do this? 我该怎么做?

Do I add in get methods in my main code and return it? 是否在我的主代码中添加get方法并返回它? Or is there another way (eg. changing the list of objects to protected/public?) 还是有另一种方法(例如,将对象列表更改为受保护/公共对象?)

Thanks! 谢谢!

Code's supposed to be 3 seperate classes, Main, MovieDatabase & Movie. 代码应该是3个单独的类,即Main,MovieDatabase和Movie。

An instance of movieDatabase is initialized in Main. movieDatabase的实例在Main中初始化。 Upon construction, it calls loadMovieList() and populates the list from a text file. 构建后,它将调用loadMovieList()并从文本文件填充列表。 However I wish to call the same instantiation of movieDatabase from another class in order to access the movies, so that I do not have to repeat the loading. 但是,我希望从另一个类中调用movieDatabase的相同实例化,以便访问电影,因此不必重复加载。

public class Main {

public static void main(String[] args) {
    MovieDatabase movieDatabase = new MovieDatabase();
}

public class MovieDatabase {
ArrayList<Movie>movieList = new ArrayList<Movie>();
String fileAddress = "D:/Users/Mine/School/Java/CZ2002_Assignment/src/MovieDatabase/movieDatabase.txt";

public MovieDatabase()
{
    numOfMovie=0;
    loadMovieList();
}

public int getNumOfMovie() {
    return numOfMovie;
}

public void addMovieToList(Movie movie) {
    movieList.add(movie);
    numOfMovie++;
}

public Movie selMovieByID(int movieID) {
    int index=-1;
    for (Movie m : movieList) {
            index++;   
            if (m.getMovieID() == movieID)
                break;
    }
    return selMovieByIndex(index);
}


public Movie selMovieByIndex(int index) {
    return movieList.get(index);
}

public void loadMovieList()
{
    //loads through text file
    addMovieToList(new Movie(tempMovie));
    System.out.println("Movie Database loaded");
}

public class Movie{
private int movieID;
private String movieName;
private int movieDuration;  //in minutes;
private String movieRating; //G; PG; PG13; NC16; M18; R21;
private boolean has3D;
private boolean status; 
}

如果您有一个依赖于NameLibrary的类,则应通过构造函数或set方法注入它。

Firstly, its difficult to assess what issues you truly have without any code to show us. 首先,很难在没有任何代码向我们展示的情况下评估您真正遇到的问题。 However you mention main method, as in public static void main(String args[]){}; 但是,您提到了main方法,就像在公共静态void main(String args []){};中一样。

this main method is designed specifically to run the application, your compiler needs that specific method, it is not designed to be used as an accessor method eg 这个主要方法是专门为运行应用程序而设计的,您的编译器需要该特定方法,而不是用作访问器方法,例如

public int getValue(){
return value;}

this is not the only reason you can't access the main method variable. 这不是不能访问主方法变量的唯一原因。 main doesn't have a return type (due to the use of void) plus the idea of SCOPE (each method has a scope, any method that contains a variable can see that variable, but nothing outside of it can directly see it without a return type) you use scope to limit what can be accessed or what cannot be accessed outside of the methods or classes (thats why class variables usually will have private, in order to limit accessibility) main没有返回类型(由于使用了void),加上SCOPE的想法(每个方法都有一个作用域,任何包含变量的方法都可以看到该变量,但是没有它,没有它可以直接看到它)返回类型),您可以使用作用域来限制在方法或类之外可以访问的内容或不能访问的内容(这就是为什么类变量通常具有私有属性以限制可访问性)

Create a getter-method which returns the list inside your NameLibrary . 创建一个getter方法,该方法返回NameLibrary的列表。 if your other class extends from NameLibrary you can call this getter-method with the object reference to your NameLibrary class. 如果您的其他类是从NameLibrary扩展的, NameLibrary可以将此getter方法与对NameLibrary类的对象引用一起NameLibrary

If you want int x to be accessible from other classes, you write: 如果希望其他类可以访问int x ,请编写:

public class myClass{
    public int x = 0;
}

To access it from other classes, you simply write: 要从其他类访问它,只需编写:

myClass.x ... (do something)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM