java编写一个book类
时间: 2024-02-06 12:10:00 浏览: 27
以下是一个简单的 Book 类的 Java 编写示例:
```java
public class Book {
private String title;
private String author;
private int publicationYear;
private String publisher;
private double price;
public Book(String title, String author, int publicationYear, String publisher, double price) {
this.title = title;
this.author = author;
this.publicationYear = publicationYear;
this.publisher = publisher;
this.price = price;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public int getPublicationYear() {
return publicationYear;
}
public String getPublisher() {
return publisher;
}
public double getPrice() {
return price;
}
public void setTitle(String title) {
this.title = title;
}
public void setAuthor(String author) {
this.author = author;
}
public void setPublicationYear(int publicationYear) {
this.publicationYear = publicationYear;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public void setPrice(double price) {
this.price = price;
}
}
```
这个类包含了书籍的基本属性:书名、作者、出版年份、出版社和价格。并提供了相应的 getter 和 setter 方法来访问和修改这些属性。在构造函数中初始化对象的属性。