类型不匹配无法将一种类型转换为另一种类型

时间:2017-03-05 08:06:31

标签: java

所以我有两个类,一个Item类和一个从Item扩展的Book类。理论上,Book对象也应该被视为Item对象。我有一个书店,有一个集合,想要存储书籍。 它给出了这个错误: 类型不匹配:无法从Collection Item转换为Collection Book

如何制作它以便将Books识别为Item。应该可以替换为Item a = new Book();谢谢!

BookStore类:

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);
    }
}

书类是这样的:

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;
}

项目类是:

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();
}

1 个答案:

答案 0 :(得分:2)

BookStore课程中,您正在呼叫

 Collection<Book> books = getCollectionOfItems();

返回Item的集合,注意Book可以投放到Item而不是反过来。所以你需要改变上面的

Collection<Item> books = getCollectionOfItems();

如果您想要显示所有图书,请在显示之前迭代所有项目并检查当前项目是否为图书,

public void displayAllBooksWrittenByAuthorsOverThisAge(int ageInYears) {
   Iterator<Item> items = getCollectionOfItems().iterator(); 
   while (it.hasNext()) {
      Item item = it.next();
      if(item instanceof Book){
         displayBook((Book)item);
      }
   }
}