老师帮我看一下代码哪里出错了?
来源:1-7 编程练习
慕容2015448
2019-10-21 17:07:59
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 getName(){
return name;
}
//作者只读
public String getAuthor(){
return author;
}
//出版社读取
public String getPress(){
return press;
}
//出版社写入
public void setPress(String press){
this.press=press;
}
//价格读取
public double getPrice(){
return price;
}
//价格写入,如果价格小于10元,则提示“图书价格最低10元”,并强制把价格赋值10
public void setPrice(double price){
if(price<10){
System.out.println("图书价格最低10元");
this.price=10;
}else{
this.price=price;
}
}
//信息介绍方法,描述图书所有信息
public void messgeBook(){
System.out.println("书名:"+getName());
System.out.println("作者:"+getAuthor());
System.out.println("出版社:"+getPress());
System.out.println("价格:"+getPrice());
}
}1回答
好帮手慕小班
2019-10-21
同学你好,复制运行贴出代码,有如下问题和建议:
1、根据题目要求, 限定作者、书名均为只读属性,所以建议去掉Book类中的出版社与价格的get方法。
对应在messgeBook方法中,对应可以通过直接获取price来得到这个价格属性呐。
2、 book.getPrice();是获取到book的price属性,对应需要有一个变量来接收这个方法的返回值,例如:

3、这里同学是否是调用方法调用有误呐,应该是调用messgeBook方法呐,

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
相似问题