编写java代码实现类图书目书本一对n
时间: 2024-06-15 09:07:59 浏览: 15
要实现类图书目书本一对n的关系,可以使用Java中的面向对象编程来实现。以下是一种可能的实现方式:
首先,我们需要定义两个类:Book和Library。
Book类表示一本书,它包含书的属性,如书名、作者、出版社等。同时,Book类还需要一个方法来获取书的信息。
```java
public class Book {
private String title;
private String author;
private String publisher;
public Book(String title, String author, String publisher) {
this.title = title;
this.author = author;
this.publisher = publisher;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public String getPublisher() {
return publisher;
}
public String getInfo() {
return "Title: " + title + ", Author: " + author + ", Publisher: " + publisher;
}
}
```
Library类表示图书馆,它包含一个Book类型的数组来存储图书馆中的书籍。同时,Library类还需要一些方法来管理图书馆中的书籍,如添加书籍、删除书籍等。
```java
public class Library {
private Book[] books;
private int count;
public Library(int capacity) {
books = new Book[capacity];
count = 0;
}
public void addBook(Book book) {
if (count < books.length) {
books[count] = book;
count++;
System.out.println("Book added successfully.");
} else {
System.out.println("Library is full. Cannot add more books.");
}
}
public void removeBook(Book book) {
boolean found = false;
for (int i = 0; i < count; i++) {
if (books[i].equals(book)) {
found = true;
for (int j = i; j < count - 1; j++) {
books[j] = books[j + 1];
}
count--;
System.out.println("Book removed successfully.");
break;
}
}
if (!found) {
System.out.println("Book not found in the library.");
}
}
public void displayBooks() {
if (count == 0) {
System.out.println("Library is empty.");
} else {
System.out.println("Books in the library:");
for (int i = 0; i < count; i++) {
System.out.println(books[i].getInfo());
}
}
}
}
```
现在,我们可以创建Book对象并将其添加到Library中:
```java
public class Main {
public static void main(String[] args) {
Book book1 = new Book("Java Programming", "John Smith", "ABC Publisher");
Book book2 = new Book("Python Basics", "Jane Doe", "XYZ Publisher");
Library library = new Library(10);
library.addBook(book1);
library.addBook(book2);
library.displayBooks();
}
}
```
这样,我们就实现了类图书目书本一对n的关系。通过Library类,我们可以管理多本书籍,并进行添加、删除和展示等操作。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)