用JAVA写一个图书类book
时间: 2024-12-21 19:23:03 浏览: 17
在Java中,我们可以创建一个名为`Book`的类来表示一本书,包含一些基本属性如书名、作者、出版社、出版年份等。下面是一个简单的`Book`类示例:
```java
public class Book {
private String title; // 书籍标题
private String author; // 作者名称
private String publisher; // 出版社
private int publicationYear; // 出版年份
// 构造函数
public Book(String title, String author, String publisher, int publicationYear) {
this.title = title;
this.author = author;
this.publisher = publisher;
this.publicationYear = publicationYear;
}
// 获取和设置属性的方法
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 String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public int getPublicationYear() {
return publicationYear;
}
public void setPublicationYear(int publicationYear) {
this.publicationYear = publicationYear;
}
@Override
public String toString() {
return "Book{" +
"title='" + title + '\'' +
", author='" + author + '\'' +
", publisher='" + publisher + '\'' +
", publicationYear=" + publicationYear +
'}';
}
}
```
在这个类中,我们定义了私有变量和对应的getter/setter方法,以及一个`toString()`方法用于展示书籍的信息。如果有需要的话,还可以添加更多的方法,比如借阅记录或者价格信息。
阅读全文