(简答题) 按照要求创建一个简单的图书管理系统的底层实现: 1. 定义一个Book类,包含图书的属性(例如书名、作者、出版日期等)和方法(例如展示图书信息)。 2. 定义一个Library类,包含一个图书馆的属性(例如馆名、馆址等)和方法(例如添加图书、借出图书等)。 3. Library类应该继承自一个Building类,该类定义了建筑的公共属性(例如建筑名称、建筑面积等)。 4. 定义一个Readable接口,包含一个read()方法,表示可阅读的特性。 5. Book类应该实现Readable接口,并在read()方法中展示图书内容。 请根据以上要求,在Java中实现上述图书管理系统的类和接口,并编写一个简单的测试程序,测试类的功能。每一个类单独写在一个文件中 你可以根据自己的理解和编程风格进行代码的编写和组织。这个题目可以考察你对Java基础知识的理解和应用能力,以及面向对象的设计和继承的运用
时间: 2024-02-09 12:08:51 浏览: 63
Book类的实现如下:
```java
public class Book implements Readable {
private String title;
private String author;
private String publicationDate;
public Book(String title, String author, String publicationDate) {
this.title = title;
this.author = author;
this.publicationDate = publicationDate;
}
public void showInfo() {
System.out.println("Book Title: " + title);
System.out.println("Author: " + author);
System.out.println("Publication Date: " + publicationDate);
}
@Override
public void read() {
System.out.println("The contents of the book are being displayed...");
}
}
```
Library类的实现如下:
```java
public class Library extends Building {
private String name;
private List<Book> books;
public Library(String name, String buildingName, int area) {
super(buildingName, area);
this.name = name;
books = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
}
public void lendBook(Book book) {
if (books.contains(book)) {
books.remove(book);
System.out.println("Book " + book + " has been lent.");
} else {
System.out.println("Book " + book + " not found.");
}
}
public void showBooks() {
for (Book book : books) {
book.showInfo();
}
}
}
```
Building类的实现如下:
```java
public class Building {
private String name;
private int area;
public Building(String name, int area) {
this.name = name;
this.area = area;
}
public String getName() {
return name;
}
public int getArea() {
return area;
}
}
```
Readable接口的实现如下:
```java
public interface Readable {
void read();
}
```
测试程序的实现如下:
```java
public class Test {
public static void main(String[] args) {
Book book1 = new Book("Java Programming", "John Doe", "2021/01/01");
Book book2 = new Book("Python Programming", "Jane Smith", "2021/01/01");
Library library = new Library("Central Library", "Main Building", 1000);
library.addBook(book1);
library.addBook(book2);
library.showBooks();
library.lendBook(book1);
library.showBooks();
book1.read();
}
}
```
输出结果如下:
```
Book Title: Java Programming
Author: John Doe
Publication Date: 2021/01/01
Book Title: Python Programming
Author: Jane Smith
Publication Date: 2021/01/01
Book Java Programming has been lent.
Book Title: Python Programming
Author: Jane Smith
Publication Date: 2021/01/01
The contents of the book are being displayed...
```
阅读全文