返回数组的类

时间:2014-01-15 19:56:24

标签: java arrays return

我对java非常新。我试图创建一个类,返回一些关于几部电影的信息(所有这些信息都存储在数组中)。我卡住了,不知道该怎么办。这是我的代码

电影课程:

public class Movie {

    String[] Director;
    String[] Name;
    String[] realeaseDate;
    String[] lastShow;


    public Movie()
    {
        String[] Director={"George Romero","Woody Allen","Steven Speilberg","James Cameron"};
        String[] Name={"Diary of the Dead","Midnight in Paris","War of the Worlds","Terminator 2 - Judgment Day"};
        String[] realeaseDate={"Dec 31 1999","Dec 28 1999","Dec 15 1999","Dec 10 1999"};
        String[] lastShow={"Jan 13 2000","Jan 29 2000","Jan 23 2000","Jan 15 2000"};

    }

    public String getDirector()
    {
        return Director;
    }

    public String getName()
    {
        return Name;
    }

    public String getRealease()
    {
        return realeaseDate;
    }

    public String getLast()
    {
        return lastShow;
    }

}

现在我的主要是:

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub


        String newLine = System.getProperty("line.separator");
        Movie movies = new Movie();

        System.out.println("Avaliable movies"+newLine);

        System.out.println("Director: "+ movies.getDirector()+newLine+"Name :"+ movies.getName()+ newLine + "Realease Date: "+ movies.getRealease()+newLine+"Last Show :"+ movies.getLast()+newLine);

    }

}

我希望结果如下:

所有可用的电影

...乔治 ...的日记 十二月.. januar ...

史蒂芬.. sdafsda ... 十二月... 扬..

。 。

1 个答案:

答案 0 :(得分:4)

由于您不熟悉java,我还建议将电影类视为单个对象(不是电影数组),然后将值存储在电影对象列表中。这样,每个电影对象仅包含有关单个电影的信息。这将是更加面向对象的方法

public class Movie {

    String Director;
    String Name;
    String releaseDate;
    String lastShow;


    public Movie(String director, String name, String release, String lastShow)
    {
        this.Director = director;
        this.Name = name;
        this.releaseDate = release;
        this.lastShow = lastShow;
    }

    public String getDirector()
    {
        return Director;
    }

    public String getName()
    {
        return Name;
    }

    public String getRelease()
    {
        return releaseDate;
    }

    public String getLast()
    {
        return lastShow;
    }

}

然后您的主文件可能如下所示:

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub


        String newLine = System.getProperty("line.separator");
        Movie firstMovie= new Movie("George Romero","Diary of the Dead", "Dec 31 1999","Jan 13 2000" );
        Movie secondMovie = new Movie("test", "name", "date", "date");
        ArrayList<Movie> movies = new ArrayList<Movie>();
        //add movies to list

        System.out.println("Avaliable movies"+newLine);

        //loop through each movie in movies

        //print information about each movie

    }

}

我将把剩下的实施留给你练习,但这应该指向正确的方向。