类返回数组 [英] Class that returns arrays
问题描述
林非常java的非常新。我试着去做出返回关于几部电影(所有这些信息都存储在阵列)一些信息的类。林卡住,不知道该怎么做。这里是我的code
Im very very new to java. Im trying to make a class that returns some info about a few movies (all this info is stored in array). Im stuck and dont know what to do. Here is my code
电影类:
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;
}
}
现在,这里是我公司主营:
Now here is my Main:
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);
}
}
我想要的结果是这样的:
I want the result to be like:
所有可用的电影
乔治...
日记...
十二月..
januar ...
George... Diary of... dec.. januar...
史蒂芬..
sdafsda ...
十二月...
扬..
Steven.. sdafsda... Dec... Jan..
。
。
. . .
推荐答案
既然你是新来的Java我也建议治疗电影类作为一个单一的对象(而不是电影中的数组),然后将值存储在一个列表电影的对象。这样,每个影片对象包含只是一部电影的信息。这将是更加面向对象的方法
Since you are new to java I would also recommend treating the movie class as a single object(not an array of movies) and then store the values in a list of movie objects. This way each movie object contains just the information about a single movie. This would be the more object oriented approach
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;
}
}
然后你的主文件可能如下所示:
And then your main file might look like the following:
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
}
}
我会离开的实现了的休息,锻炼你,但这个应该指向你在正确的方向。
I will leave the rest of the implementation up to an exercise for you, but this should point you in the right direction.
这篇关于类返回数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!