2014-01-21 18 views
0

我是一個完整的新手,當涉及到編程和我的第一篇文章時,請提前原諒我所有的錯誤。我想弄清楚爲什麼當我從一個包含超類和子類變量的數組中打印信息時,我會得到null和其他行話。我知道這與我添加的Ebook子類具體有關,但似乎在過去的幾天中完全停滯不前。我希望這裏有人不介意看看這個。提前致謝!帶有類和子類的數組null錯誤

import java.util.Arrays; 
import java.text.NumberFormat; 
import java.util.Locale; 
import java.text.DecimalFormat; 

public class Bookstore2_3 
{ 
public static void main(String args[]) 
{ 

    NumberFormat usCurrency = NumberFormat.getCurrencyInstance(Locale.US); 
    DecimalFormat simpleformat = new DecimalFormat("#,###.#"); 

    String[] titles; 
    titles = new String [5]; 

    titles [0] = new String ("David Goes to School"); 
    titles [1] = new String ("No David!"); 
    titles [2] = new String ("Simple Abundance"); 
    titles [3] = new String ("The very hungry caterpillar"); 
    titles [4] = new String ("We are going on a bear hunt");   

    Book sortedBooks[] = new Book[5]; 
    sortedBooks [0] = new EBook (0075260012l, "David goes to School", "David Shannon", 2010, "Shannon Rock", 12, "www.anything.com", 1); 
    sortedBooks [1] = new Book (7423540089l, "No David!", "David Shannon", 2009, "Shannon Rock", 13); 
    sortedBooks [2] = new Book (0743200616l, "Simple Abundance", "Sarah Breathnach", 2009, "Scribner", 15); 
    sortedBooks [3] = new EBook (78137521819l, "The very hungry caterpillar", "Eric Carle", 2005, "Philomel Books", 14, "http://www.tinyurl.fguopt8u90", 1); 
    sortedBooks [4] = new Book (9781416987116l, "We are going on a bear hunt", "Michael Rosen", 2009, "McElderry", 16); 

    System.out.println(Arrays.toString(sortedBooks)); 

} 
} // end class Bookstore 

// Begin class Book 
class Book 
{ 
public float isbn; 
public String title; 
public String authorName; 
public float yearPublished; 
public String publisherName; 
public float price; 

public Book() 
{ 
    isbn = 0; 
    title = ""; 
    authorName = ""; 
    yearPublished = 0; 
    publisherName = ""; 
    price = 0; 
} 

public Book (float bookisbn, String bookTitle, String bookauthorName, float bookyearPublished, String bookpublisherName, float bookPrice) 
{ 
    isbn = bookisbn; 
    title = bookTitle; 
    authorName = bookauthorName; 
    yearPublished = bookyearPublished; 
    publisherName = bookpublisherName; 
    price = bookPrice; 
} 


/////////////////////////////////////////////// 
public void setISBN (float ISBN) //set ISBN 
{ 
    this.isbn = ISBN; 
} 
public float getISBN() //get ISBN 
{ 
    return isbn; 
} 
////////////////////////////////////////////// 
public void setTitle (String Title) //set Title 
{ 
    this.title = Title; 
} 
public String getTitle() //get Title 
{ 
    return title; 
} 
/////////////////////////////////////////////// 
public void setAuthorName (String AuthorName) //set AuthorName 
{ 
    this.authorName = AuthorName; 
} 
public String getAuthorName() //get AuthorName 
{ 
    return authorName; 
} 
/////////////////////////////////////////////// 
public void setYearPublished (float YearPublished)//set YearPublished 
{ 
    this.yearPublished = YearPublished; 
} 
public float getYearPublished() //get YearPublished 
{ 
    return yearPublished; 
} 
/////////////////////////////////////////////// 
public void setPublisherName (String PublisherName) 
{ 
    this.publisherName = PublisherName; 
} 
public String getPublisherName() 
{ 
    return publisherName; 
} 
/////////////////////////////////////////////// 
public void setPrice (float Price) 
{ 
    this.price = Price; 
} 
public float getPrice() 
{ 
    return price; 
} 

} // end class Book 

//Begin class EBook 
class EBook extends Book 
{ 
public String webSite; 
public float discountRate; 
public float discountOff; 

public EBook (float isbn, String title, String authorName, float yearPublished, String publisherName, float price, String webSite, float discountRate) 
{ 
    super(isbn, title, authorName, yearPublished, publisherName, price); 
    webSite = ""; 
    discountRate = 2; 
} 
public EBook (String EBookWebSite, float EBookDiscount) 
{ 
    webSite = EBookWebSite; 
    discountRate = EBookDiscount; 
} 


public void setWebSite (String WebSite) 
{ 
    this.webSite = WebSite; 
} 
public String getWebSite() 
{ 
    return webSite; 
} 
public float discountOff (float discountRate, float price) //method to calculate discount off 
{ 
    float discountOff = discountRate * price; 
    return discountRate * price; 
} 
public String toString() 
{ 
    return "ISBN: " + isbn + "\n" + "Title: " + title + "\n" + "Author's Name: " + authorName + "\n" + "Year Published: " + yearPublished + "\n" + "Publisher's Name: " + publisherName + "\n" + "Price: " + price + "\n" + "Website: " + webSite + "\n" + "Discount: " + discountRate + "\n"; 
} 

} 
+0

請更具體的錯誤比「null和其他行話」。 – andars

+0

不要忘記重寫'Book'類中的'toString()'方法。 –

+0

哇!大家好,感謝您的真正快速反饋!我想要做的是顯示數組中的所有信息。它可以正確編譯,但是在實際運行時,任何帶有網站的響應都會返回空值,折扣表示爲零。網站和折扣是特定於電子書的子類的成員,所以我認爲它與此有關,我只是堅持如何解決這個問題。 – CoShark

回答

1

在你的構造

webSite = ""; 

應該

this.webSite = ""; 

this.webSite = webSite; 

以前你引用一個名爲webSite的構造函數的參數,而不是實例領域。因此,實例字段仍爲null,這是引用類型的實例變量的默認值。

+0

哇,我真不敢相信這就是爲此感謝你! – CoShark

+0

@CoShark Book @ 1d7e8c5b是所有類繼承的Object#toString()方法的結果。你需要重寫它。 –

+0

謝謝你指出,我只是想弄清楚現在的覆蓋概念。這將被放置在哪裏?如果你不介意給我看。 – CoShark