所以我有兩個類,項目類而這從項目擴展Book類。理論上,Book對象也應該被視爲Item對象。我有一個書店有一個集合,並希望存儲書籍。 它給這個錯誤: 類型不匹配:不能從收集項目轉換爲藏書類型不匹配無法一種類型轉換爲另一種
我如何使它所以它識別書籍項目也是如此。應該能夠替代項目a =新書();謝謝!
圖書城等級:
public class Book extends Item {
private Author author;
private Date datePublished;
private String title;
private BookType genre;
public Book(double weightKg, double manufacturingPriceDollars, double suggestedRetailPriceDollars, String uniqueID,
Author author, Date datePublished, String title, BookType genre) {
super(weightKg, manufacturingPriceDollars, suggestedRetailPriceDollars, uniqueID);
this.author = author;
this.datePublished = datePublished;
this.title = title;
this.genre = genre;
}
Item類是這樣的:
public class BookStore extends Store {
private BookType specialty;
public BookStore(Address address, String name, BookType specialty) {
super(address, name);
this.setSpecialty(specialty);
addBooks();
}
public void displayAllBooksWrittenByAuthorsOverThisAge(int ageInYears) {
Collection<Book> books = getCollectionOfItems(); // error type mismatch cannot convert from Collection<Item to Collection<Book>
Iterator<Book> it = books.iterator();
boolean displayedSome = false;
while (it.hasNext()) {
Book b = it.next();
int ageYears = b.getDatePublished().getYear() - b.getAuthor().getBirthDate().getYear();
if (ageYears > ageInYears) {
System.out.println(b.getTitle() + " was written by " + b.getAuthor().getName().getLastName()
+ " at age " + ageYears + ", which is more than " + ageInYears);
displayedSome = true;
}
}
if (displayedSome == false) {
System.out.println("No books by authors over age " + ageInYears);
}
}
Book類這是
public class Item {
private double weightKg;
private double manufacturingPriceDollars;
private double suggestedRetailPriceDollars;
private String uniqueID;
public Item(double weightKg, double manufacturingPriceDollars, double suggestedRetailPriceDollars,
String uniqueID) {
this.weightKg = weightKg;
this.manufacturingPriceDollars = manufacturingPriceDollars;
this.suggestedRetailPriceDollars = suggestedRetailPriceDollars;
this.uniqueID = uniqueID;
}
商店類別:
public class Store {
private Address streetAddress; // instance variable of the street address.
private String name; // name of the store.
private HashMap<String, Item> itemsForSale; // store's products for sale.
/**
* Constructor of Store.
*/
public Store(Address streetAddress, String name) {
this.streetAddress = streetAddress;
this.name = name;
itemsForSale = new HashMap<>();
}
public void addItem(Item item) {
itemsForSale.put(item.getUniqueID(), item);
}
/**
* method that gets the item when entering key
*
* @param key
* @return itemsForSale.get(key)
*/
public Item getItemByKey(String key) {
return itemsForSale.get(key);
}
/**
* method that returns back all the items
*
* @return itemsForSale.value()
*/
public Collection<Item> getCollectionOfItems() {
return itemsForSale.values();
}
你在哪兒你的錯誤到底?張貼一致的堆棧跟蹤。 – pzaenger
我在 public void displayAllBooksWrittenByAuthorsOverThisAge(int ageInYears)方法中得到錯誤。在\t \t收藏- 書= getCollectionOfItems(); –
Bomiheko
請發佈一個[MCVE] – c0der