public interface Readable {
    void read();
}
public class Building {
    private String name;
    private double area;
    
    public Building(double area) {
        this.area = area;
    }
    
    public void showBuildingInfo() {
        System.out.println("建筑名称:" + name);
        System.out.println("建筑面积:" + area);
    }
}
import java.util.ArrayList;
import java.util.List;

public class Library extends Building {
    private String name;
    private String address;
    private List<Book> books;
    
    public Library(String name, String address, double area) {
        super(area);
        this.name = name;
        this.address = address;
        this.books = new ArrayList<Book>();
    }
    
    public void addBook(Book book) {
        books.add(book);
        System.out.println("添加图书《" + book.getTitle() + "》成功!");
    }
    
    public void borrowBook(String title) {
        for (Book book : books) {
            if (book.getTitle().equals(title)) {
                books.remove(book);
                System.out.println("借出图书《" + book.getTitle() + "》成功!");
                return;
            }
        }
        System.out.println("图书馆中没有《" + title + "》这本书!");
    }
}
public class Book implements Readable {
    private String title;
    private String author;
    private String publishDate;
    
    public Book(String title, String author, String publishDate) {
        this.title = title;
        this.author = author;
        this.publishDate = publishDate;
    }
    
    public void showBookInfo() {
        System.out.println("书名:" + title);
        System.out.println("作者:" + author);
        System.out.println("出版日期:" + publishDate);
    }
    
    @Override
    public void read() {
        System.out.println("正在阅读《" + title + "》...");
    }

    public String getTitle() {
        return this.title;
    }
}
public class TestBooks {
    public static void main(String[] args) {
        Book book1 = new Book("Java编程思想", "Bruce Eckel", "2002-12-01");
        Book book2 = new Book("Effective Java", "Joshua Bloch", "2008-05-08");
        
        Library library = new Library("北京图书馆", "北京市东城区", 10000);
        library.addBook(book1);
        library.addBook(book2);
        
        library.borrowBook("Java编程思想");
        
        book1.showBookInfo();
        book1.read();
    }
}
最后修改:2023 年 05 月 26 日
如果觉得我的文章对你有用,请随意赞赏