在编程和我的第一篇文章中,我是一个完整的新手,所以请提前原谅我的所有错误。我试图找出为什么当我从包含来自超类和子类的变量的数组中打印信息时,我得到null和其他行话。我知道这特别与我添加的Ebook子类有关,但似乎在最近几天完全陷入困境。我希望有人不介意看看这个。提前谢谢!
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 :(得分:1)
在你的构造函数
中webSite = "";
应该是
this.webSite = "";
或
this.webSite = webSite;
以前,您引用了名为webSite
的构造函数参数,而不是实例字段。因此,实例字段保持null
,这是引用类型的实例变量的默认值。