1-9 编程练习打卡,请老师检查
来源:1-9 编程练习
慕尼黑3413982
2021-01-02 14:54:18
问题描述:
老师,我这代码正确么,还有什么逻辑上面的遗漏没有,请指点
相关截图:

相关代码:
public class Book {
//私有属性:书名、作者、出版社、价格
private String title;
private String author;
private String publish;
private double price;
//通过构造方法实现属性赋值
public Book(String title,String author,String publish,double price){
this.title=title;
this.author=author;
this.setPublish(publish);
this.setPrice(price);
}
/*通过公有的get/set方法实现属性的访问,其中:
1、限定图书价格必须大于10,如果无效需进行提示,并强制赋值为10
2、限定作者、书名均为只读属性
*/
public String getTitle(){
return this.title;
}
public String getAuthor(){
return this.author;
}
public String getPublish(){
return this.publish;
}
public void setPublish(String publish){
this.publish=publish;
}
public Double getPrice(){
return this.price;
}
public void setPrice(double price){
if(price<10.0){
System.out.println("图书价格最低10元");
price=10.0;
this.price=price;
}else{
this.price=price;
}
}
//信息介绍方法,描述图书所有信息
public void introduce(String title,String author,String publish,double price){
System.out.println("书名:"+title);
System.out.println("作者:"+author);
System.out.println("出版社:"+publish);
System.out.println("价格:"+price);
}
}相关代码:
public class BookTest {
// 测试方法
public static void main(String[] args) {
//实例化对象,调用相关方法实现运行效果
Book one,two;
one=new Book("红楼梦","曹雪芹","人民文学出版社",8);
two=new Book("小李飞刀","古龙","中国长安出版社",55.5);
one.introduce(one.getTitle(),one.getAuthor(),one.getPublish(),one.getPrice());
System.out.println("================");
two.introduce(one.getTitle(),one.getAuthor(),one.getPublish(),one.getPrice());
}
}1回答
同学你好,1. 展示的都是one对象中的信息,建议同学也展示two对象中的信息。

2. 信息介绍方法,建议创建为无参方法,修改后代码如下所示:

祝学习愉快!
相似问题