gpt4 book ai didi

java - 具有类和子类空错误的数组

转载 作者:行者123 更新时间:2023-12-01 13:36:16 25 4
gpt4 key购买 nike

在编程和我的第一篇文章方面,我是一个完全的新手,所以请提前原谅我的所有错误。我试图弄清楚为什么当我从包含父类(super class)和子类变量的数组中打印信息时,我得到 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";
}

}

最佳答案

在你的构造函数中

webSite = "";

应该是

this.webSite = ""; 

this.webSite = webSite;

之前您引用的是名为 webSite 的构造函数参数,而不是实例字段。因此,实例字段仍为 null,这是引用类型实例变量的默认值。

关于java - 具有类和子类空错误的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21247434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com