老师来检查作业啦~
public class Book {
//私有属性:书名、作者、出版社、价格
private String name;
private String author;
private String publisher;
private double price;
//通过构造方法实现属性赋值
public Book(String name,String author,String publisher,double price){
this.name = name;
this.author = author;
this.publisher = publisher;
if(price > 10){
this.price = price;
}else{
this.price = 10;
}
}
public Book(){}
/*通过公有的get/set方法实现属性的访问,其中:
1、限定图书价格必须大于10,如果无效需进行提示,并强制赋值为10
2、限定作者、书名均为只读属性
*/
public String getName(){
return this.name;
}
public String getAuthor(){
return this.author;
}
public String getPublisher(){
return this.publisher;
}
public double getPrice(){
return this.price;
}
public void setPublisher(String publisher){
this.publisher = publisher;
}
public void setPrice(double price){
if(price > 10){
this.price = price;
}else{
this.price = 10;
}
}
//信息介绍方法,描述图书所有信息
public void introduce(){
if(price <= 10){
System.out.println("图书价格最低10元");
}
System.out.println("书名:"+name);
System.out.println("作者:"+author);
System.out.println("出版社:"+publisher);
System.out.println("价格:"+price);
System.out.println("=====================");
}
}2
收起
正在回答
1回答
同学你好!
整体完成不错,但是还有以下优化的空间:
同学在构造方法中以及set方法中都对价格进行了校验,这里老师建议同学将构造方法中的去掉,只保留set方法中的校验
去掉之后,同学在构造方法中调用setPrice(double price)方法对价格赋值即可
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星