编写book类,该类定义了三个属性,分别是title表示书名,author表示作者,price表示价格,三个set方法分别用来设置书名、作者和价格的值,三个get方法分别用来获取书名,作者和价格。编写两个类测试类test,用来测试book类,创建book对象并输出其属性的值。
时间: 2023-05-23 16:06:33 浏览: 572
A:
```java
// Book类
public class Book {
private String title; // 书名
private String author; // 作者
private double price; // 价格
// 构造方法
public Book(String title, String author, double price) {
this.title = title;
this.author = author;
this.price = price;
}
// set方法
public void setTitle(String title) {
this.title = title;
}
public void setAuthor(String author) {
this.author = author;
}
public void setPrice(double price) {
this.price = price;
}
// get方法
public String getTitle() {
return this.title;
}
public String getAuthor() {
return this.author;
}
public double getPrice() {
return this.price;
}
}
// test1类
public class test1 {
public static void main(String[] args) {
Book book = new Book("Java从入门到精通", "张三", 58.5);
System.out.println("书名:" + book.getTitle() + ",作者:" + book.getAuthor() + ",价格:" + book.getPrice());
book.setTitle("Java编程思想");
book.setAuthor("Bruce Eckel");
book.setPrice(89.0);
System.out.println("书名:" + book.getTitle() + ",作者:" + book.getAuthor() + ",价格:" + book.getPrice());
}
}
// test2类
public class test2 {
public static void main(String[] args) {
Book book = new Book("JavaWeb开发技术", "李四", 78.0);
System.out.println("书名:" + book.getTitle() + ",作者:" + book.getAuthor() + ",价格:" + book.getPrice());
book.setPrice(99.9);
System.out.println("书名:" + book.getTitle() + ",作者:" + book.getAuthor() + ",价格:" + book.getPrice());
}
}
```