图书bean: Book.java 包含属性:图书ID,书名,作者,价格,出版社,库存数,图书照片路径,请编写代码使其初始化生成一些图书数据
时间: 2025-01-28 16:14:16 浏览: 33
好的,我来为您编写一个Book.java的代码,其中包含图书的各个属性,并初始化一些图书数据。以下是代码实现:
```java
public class Book {
private int bookId;
private String title;
private String author;
private double price;
private String publisher;
private int stock;
private String imagePath;
// 构造方法
public Book(int bookId, String title, String author, double price, String publisher, int stock, String imagePath) {
this.bookId = bookId;
this.title = title;
this.author = author;
this.price = price;
this.publisher = publisher;
this.stock = stock;
this.imagePath = imagePath;
}
// Getter 和 Setter 方法
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
public String getImagePath() {
return imagePath;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
// toString 方法
@Override
public String toString() {
return "Book{" +
"bookId=" + bookId +
", title='" + title + '\'' +
", author='" + author + '\'' +
", price=" + price +
", publisher='" + publisher + '\'' +
", stock=" + stock +
", imagePath='" + imagePath + '\'' +
'}';
}
// 主方法,初始化一些图书数据
public static void main(String[] args) {
Book[] books = new Book[5];
books[0] = new Book(1, "Java编程思想", "Bruce Eckel", 99.0, "机械工业出版社", 100, "images/java_thinking.jpg");
books[1] = new Book(2, "深入理解计算机系统", "Randal E. Bryant", 129.0, "电子工业出版社", 50, "images/computer_systems.jpg");
books[2] = new Book(3, "算法导论", "Thomas H. Cormen", 150.0, "高等教育出版社", 80, "images/introduction_to_algorithms.jpg");
books[3] = new Book(4, "设计模式", "Erich Gamma", 119.0, "机械工业出版社", 60, "images/design_patterns.jpg");
books[4] = new Book(5, "代码大全", "Steve McConnell", 89.0, "电子工业出版社", 120, "images/code_complete.jpg");
// 打印图书信息
for (Book book : books) {
System.out.println(book);
}
}
}
```
这段代码定义了一个Book类,包含您提到的所有属性,并提供了构造方法、Getter和Setter方法、toString方法以及一个main方法用于初始化一些图书数据并打印出来。
阅读全文
相关推荐

















