用JAVA语言定义Book类,完成属性声明、属性相应的get和set方法、包含所有属性的构造方法
时间: 2023-06-18 10:06:05 浏览: 54
下面是一个用Java语言定义的Book类,包含属性声明、属性相应的get和set方法、包含所有属性的构造方法:
```
public class Book {
private String title;
private String author;
private int year;
private double price;
public Book(String title, String author, int year, double price) {
this.title = title;
this.author = author;
this.year = year;
this.price = price;
}
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 int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
```
在这个类中,我们定义了四个私有属性:title、author、year和price。我们还定义了一个构造函数,它接受四个参数,分别是书的标题、作者、出版年份和价格。此外,我们还定义了四个公共方法,用于获取和设置这些属性的值。这些方法分别是getTitle()、setTitle()、getAuthor()、setAuthor()、getYear()、setYear()、getPrice()和setPrice()。