创建图书类,包含书名,书号,作者,出版社,出版日期,价格,并实现图书信息的输出
时间: 2023-08-16 12:07:38 浏览: 298
好的,根据您的需求,我为您提供一个Java实现的图书类,代码如下:
```java
public class Book {
private String bookName; // 书名
private String bookNo; // 书号
private String author; // 作者
private String publisher; // 出版社
private String publishDate; // 出版日期
private double price; // 价格
public Book(String bookName, String bookNo, String author, String publisher, String publishDate, double price) {
this.bookName = bookName;
this.bookNo = bookNo;
this.author = author;
this.publisher = publisher;
this.publishDate = publishDate;
this.price = price;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookNo() {
return bookNo;
}
public void setBookNo(String bookNo) {
this.bookNo = bookNo;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getPublishDate() {
return publishDate;
}
public void setPublishDate(String publishDate) {
this.publishDate = publishDate;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public void display() {
System.out.println("书名:" + bookName);
System.out.println("书号:" + bookNo);
System.out.println("作者:" + author);
System.out.println("出版社:" + publisher);
System.out.println("出版日期:" + publishDate);
System.out.println("价格:" + price);
}
}
```
这个类有一个构造方法和一些getter和setter方法,以及一个display方法,可以输出图书的信息。在使用时,可以先创建一个Book对象,然后调用display方法输出信息,例如:
```java
Book book = new Book("Java从入门到精通", "978-7-121-32954-6", "张三", "电子工业出版社", "2020-01-01", 68.8);
book.display();
```
输出结果如下:
```
书名:Java从入门到精通
书号:978-7-121-32954-6
作者:张三
出版社:电子工业出版社
出版日期:2020-01-01
价格:68.8
```
阅读全文