-->

类返回数组(Class that returns arrays)

2019-10-19 05:42发布

林非常非常新到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 ... ...月...一月

。 。 。

Answer 1:

既然你是新到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

    }

}

我会离开的实施长达一个锻炼你的休息,但这个应该指向你在正确的方向。



文章来源: Class that returns arrays