老师,我的代码需要优化吗?为什么编译器会报错?

相关代码:
public class Book {
//私有属性:书名、作者、出版社、价格
private String name;
private String author;
private String press;
private double price;
//通过构造方法实现属性赋值
public Book(String name,String author,String press,double price) {
this.name=name;
this.author=author;
this.setPress(press);
this.setPrice(price);
}
/*通过公有的get/set方法实现属性的访问,其中:
1、限定图书价格必须大于10,如果无效需进行提示,并强制赋值为10
2、限定作者、书名均为只读属性
*/
public String getPress() {
return press;
}
public void setPress(String press) {
this.press = press;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
if(price<=10) {
System.out.println("图书价格最低10元");
this.price=10;
}else {
this.price = price;
}
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
//信息介绍方法,描述图书所有信息
public void methods() {
System.out.println("书名:"+this.getName());
System.out.println("作者:"+this.getAuthor());
System.out.println("出版社:"+this.getPress());
System.out.println("价格:"+this.getPrice()+"元");
}
}10
收起
正在回答 回答被采纳积分+1
1回答
2023版Java工程师
- 参与学习 人
- 提交作业 8790 份
- 解答问题 9886 个
综合就业常年第一,编程排行常年霸榜,北上广深月薪过万! 不需要基础,无需脱产即可学习,只要你有梦想,想高薪! 全新升级:技术栈升级(包含VUE3.0,ES6,Git)+项目升级(前后端联调与功能升级)
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星