1. 定义一个Book类,包含图书的属性(例如书名、作者、出版日期等)和方法(例如展示图书信息)。 2. 定义一个Library类,包含一个图书馆的属性(例如馆名、馆址等)和方法(例如添加图书、借出图书等)。 3. Library类应该继承自一个Building类,该类定义了建筑的公共属性(例如建筑名称、建筑面积等)。 4. 定义一个Readable接口,包含一个read()方法,表示可阅读的特性。 5. Book类应该实现Readable接口,并在read()方法中展示图书内容。 请根据以上要求,在Java中实现上述图书管理系统的类和接口,并编写一个简单的测试程序,测试类的功能。每一个类单独写在一个文件中
时间: 2024-02-09 14:08:52 浏览: 268
定义一个Book类,实现简单操作实验报告
4星 · 用户满意度95%
Book类的实现:
```
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 displayInfo() {
System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("Publish Date: " + publishDate);
}
@Override
public void read() {
System.out.println("The book " + title + " is a great read!");
}
}
```
Library类的实现:
```
import java.util.ArrayList;
public class Library extends Building {
private String name;
private String address;
private ArrayList<Book> books;
public Library(String name, String address, ArrayList<Book> books, String buildingName, double area) {
super(buildingName, area);
this.name = name;
this.address = address;
this.books = books;
}
public void addBook(Book book) {
books.add(book);
}
public void lendBook(Book book) {
books.remove(book);
}
public void displayBooks() {
for (Book book : books) {
book.displayInfo();
}
}
}
```
Building类的实现:
```
public class Building {
private String name;
private double area;
public Building(String name, double area) {
this.name = name;
this.area = area;
}
public String getName() {
return name;
}
public double getArea() {
return area;
}
}
```
Readable接口的实现:
```
public interface Readable {
void read();
}
```
测试程序的实现:
```
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
Book book1 = new Book("The Great Gatsby", "F. Scott Fitzgerald", "April 10, 1925");
Book book2 = new Book("To Kill a Mockingbird", "Harper Lee", "July 11, 1960");
ArrayList<Book> books = new ArrayList<>();
books.add(book1);
books.add(book2);
Library library = new Library("Central Library", "123 Main St", books, "City Hall", 5000);
System.out.println("Library Name: " + library.getName());
System.out.println("Library Address: " + library.getAddress());
System.out.println("Building Name: " + library.getName());
System.out.println("Building Area: " + library.getArea());
library.displayBooks();
System.out.println("Lending " + book1.getTitle() + "...");
library.lendBook(book1);
System.out.println("Books after lending:");
library.displayBooks();
book1.read();
book2.read();
}
}
```
阅读全文