2014-01-15 65 views
1

對於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); 

    } 

} 

我想要的結果是這樣的:

所有可用的電影

George ... 日記... dec .. januar ...

史蒂芬.. sdafsda ... 月... 一月..

。 。 。

+0

你在你的構造函數中隱藏你的字段。同時檢查你的退貨類型。另外,請檢查如何打印數組的內容。另外,看看封裝。 –

+1

換句話說,將'String [] Director = {「George Romero」,「Woody ...'改爲'Director = {」George Romero「,」Woody ...' – aliteralmind

+1

也值得注意的是,你已經定義了一個'String [] Director',但是你的方法'getDirector()'返回一個'String'。 –

回答

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 

    } 

} 

我將離開執行其他長達一個鍛鍊你,但是這應該指出你在正確的方向。

+1

這是迄今唯一正確的答案。顯然OP是困惑OOP – Cruncher

+0

@Cruncher我同意,刪除答案。 – bblincoe

+0

@ kevin94,你應該接受這個答案。 (永遠不會太遲。) – jimhark