【问题标题】:Class that returns arrays返回数组的类
【发布时间】:2014-02-04 11:39:15
【问题描述】:

我对 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);

    }

}

我希望结果是这样的:

所有可用的电影

乔治... 日记... 十二月.. 一月...

史蒂文.. 斯达夫斯达... 十二月... 一月..

。 . .

【问题讨论】:

  • 你在构造函数中隐藏了你的字段。还要检查您的退货类型。另外,检查如何打印出数组的内容。另外,请查看封装。
  • 也就是说,把String[] Director={"George Romero","Woody ...改成Director={"George Romero","Woody ...
  • 另外值得注意的是,您已经定义了 String[] Director,但您的方法 getDirector() 返回了 String
  • 我认为你真正想要的是一系列电影...而不是包含一系列不同电影的电影

标签: java arrays return


【解决方案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

    }

}

我将把其余的实现留给你练习,但这应该会为你指明正确的方向。

【讨论】:

  • 这是迄今为止唯一正确的答案。显然,OP 对 OOP 感到困惑
  • @Cruncher 我同意,删除答案。
  • @kevin94,你应该接受这个答案。 (永远不会太晚。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-07
  • 2015-07-31
  • 1970-01-01
  • 2015-09-13
  • 2014-08-23
相关资源
最近更新 更多