2017-03-31 85 views
1

我想創建一個java程序,我有一個超類ReadingMaterial和許多不同的子類,即報紙,漫畫,小說和TextBook。現在,Readingmaterial有3個數據成員,每個其他類每個都有2個不同的數據成員。作者價值不顯示時提供

我想,以顯示我創建例如像字符串:

allMat.add(new Newspaper("NewYork_Times")); 

我希望「NewYork_Times」和日期,要顯示的作者 誰能告訴我我在做什麼錯?

PS:我在JAVA是新的編程

報紙類

public class Newspaper 
     extends ReadingMaterial { 

    private String title = "BHCC_Newspaper"; 

    //public Newspaper(){} 
    public Newspaper(String title) { 
     this.title = title; 
    } 

    public Newspaper(String title, String author, boolean readingMaterial) { 
     super(author, readingMaterial); 
     this.title = title; 
    } 

    //getter 
    public String getTitle() { 
     return title; 
    } 

    // setter 
    public void setTitle(String title) { 
     this.title = title; 
    } 

    public void displayReadingMaterials() { 
     System.out.println(getTitle() + "\n Microsoft recruited 15 students from a community college.\n"); 
    } 
} 

這是超

public class ReadingMaterial implements Comparable { 

    private String author = "Lydie"; 
    private boolean readingMaterial; 
    private java.util.Date dateCreated; 

    // no-arg constructor 
    public ReadingMaterial() { 
     dateCreated = new java.util.Date(); 
    } 
    // arg constructor 

    public ReadingMaterial(String author, boolean readingMaterial) { 
     dateCreated = new java.util.Date(); 
     this.author = author; 
     this.readingMaterial = readingMaterial; 
    } 
    // getters 

    public String getAuthor() { 
     return author; 
    } 

    public java.util.Date getDateCreated() { 
     return dateCreated; 
    } 

    public boolean getReadingMaterial() { 
     return readingMaterial; 
    } 
    // setters 

    public void setAuthor(String author) { 
     this.author = author; 
    } 

    public void setReadingMaterial(boolean readingMaterial) { 
     this.readingMaterial = readingMaterial; 
    } 

    public void displayReadingMaterials() { 
     System.out.println("Written on " + getDateCreated() + " by " + getAuthor() 
       + " on all ReadingMaterial: " + getReadingMaterial()); 
    } 

//Comparator for sorting the list 
    public static Comparator <ReadingMaterial> testCompare = new Comparator <ReadingMaterial>(){ 

      public int compare(ReadingMaterial readMat1, ReadingMaterial readMat2){ 


      return (int) (readMat1.getAuthor().compareTo(readMat2.getAuthor())); 
      } 
    }; 
    @Override 
    public String toString() { 
     return "Written on " + dateCreated + " by " + author 
       + " on all ReadingMaterial: " + readingMaterial; 
    } 

    @Override 
    public int compareTo(Object t) { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 



} 

,這是主要的

package readingmaterial; 
import java.util.*; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Iterator; 
import java.util.Collections; 


public class ReadingFactory { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 

     // create and initialize a newspaper, a manga, a textbook and a novel 

     List<ReadingMaterial> allMat = new ArrayList<ReadingMaterial>(); 
     allMat.add(new Newspaper("NewYork_Times")); 
     allMat.add(new Manga("Naruto")); 
     allMat.add(new TextBook("Bible")); 
     allMat.add(new Novel("Argonauts")); 
     //Iterator<ReadingMaterial> readMat = all.iterator(); 

     System.out.println("ArrayList elements after sorting in ascending order : "); 

     Collections.sort(allMat, ReadingMaterial.testCompare); 

     for(ReadingMaterial str: allMat){ 
      System.out.println(str); 

     //ReadingMaterial[] allMat={newspaper,manga,textbook,novel}; 
     //for(int i=0;i<4;i++){ 
      // allMat[i].displayReadingMaterials(); 
     // } 
    } 
    // this method show the polymorphic call and display any reading material 

} 
+2

在'(int)(readMat1.getAuthor()。compareTo(readMat2.getAuthor()));'是多餘的。所顯示的代碼具有不平衡的大括號,並且不會編譯。 –

+0

這個問題是問爲什麼沒有作者提供時沒有作者顯示。 –

+0

順便說一句,麻煩的Date類已經被諸如java.time.Instant之類的java.time類所取代。 –

回答

2

問題是那個你正在做new Newspaper("NewYork_Times")你正在使用默認的超級構造函數,沒有參數,這意味着沒有設置author
因此,你在你的比較獲得空指針(getAuthor返回null):

readMat1.getAuthor().compareTo(readMat2.getAuthor()) 

你應該:
1.筆者加空檢查您比較
2.如果算上比較,你不能允許它爲空。或者沒有它的構造函數,或設置一個默認值(就像你對日期做的那樣)

+0

謝謝我修復了它 –