关于作业,下列代码思路是否正确?
来源:1-7 编程练习
MAYxDAY
2020-01-13 19:16:42
public class Book {
//私有属性:书名、作者、出版社、价格
private String book;
private String authon;
private String press;
private double price;
//通过构造方法实现属性赋值
public Book(){}
public Book(String press){
this.setPress(press);
this.add();
}
/*通过公有的get/set方法实现属性的访问,其中:
1、限定图书价格必须大于10,如果无效需进行提示,并强制赋值为10
2、限定作者、书名均为只读属性
*/
public String getBook(){
return book;
}
public String getAuthob(){
return authon;
}
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元");
price=10;
} this.price=price;//别忘记用this语句
}
//信息介绍方法,描述图书所有信息
public void add(){
System.out.println("出版商:"+press);
}
public void message(){
System.out.println("价格:"+price);
}
public void cao(){
System.out.println("书名:红楼梦");
System.out.println("作者:曹雪芹");
}public void xiao(){
System.out.println("=====================");
System.out.println("书名:小李飞刀");
System.out.println("作者:古龙");
}
}
public class BookTest {
// 测试方法
public static void main(String[] args) {
//实例化对象,调用相关方法实现运行效果
Book book=new Book();
book.setPrice(9.0);
book.cao();
Book b1=new Book("人民文学出版者");
book.message();
book.setPrice(55.5);
book.xiao();
Book b2=new Book("中国长安出版社");
book.message();
}
}
1回答
好帮手慕小脸
2020-01-14
同学你好,已完成练习,但根据题意应使用“信息介绍方法描述图书所有信息”,如下图所示这种形式:
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
相似问题