gpt4 book ai didi

java - 返回数组的类

转载 作者:行者123 更新时间:2023-11-29 03:25:44 26 4
gpt4 key购买 nike

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

}

}

我希望结果是这样的:

所有可用的电影

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

史蒂文..sdafsda...十二月...一月..

...

最佳答案

由于您是 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

}

}

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

关于java - 返回数组的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21146970/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com