正在回答
4回答
主方法中第一句执行Book one=new Book("红楼梦","曹雪芹","人民文学出版社",1);调用带参构造方法,在带参构造方法中执行this.setPrice(price);时,进入setPrice()方法并且传入参数1,在setPrice()方法中判断参数price<=10.0为真,所以打印输出了“图书价格最低为10元”的提示信息。
祝学习愉快~
sa1tedFish
2018-02-01 12:28:47
public class Book {
//私有属性:书名、作者、出版社、价格
private String book;
private String writer;
private String publish;
private double price;
//通过构造方法实现属性赋值
public Book(String book,String writer,String publish,double price)
{
this.book=book;
this.writer=writer;
this.publish=publish;
this.setPrice(price);
}
public String getBook()
{
return book;
}
public String getWriter()
{
return writer;
}
public void setPublish(String publish)
{
this.publish=publish;
}
public String getPublish()
{
return publish;
}
public void setPrice(double price)
{
if(price<=10.0)
{
System.out.println("图书价格最低为10元");
this.price=10.0;
}else{
this.price=price;}
}
public double getPrice()
{
return price;
}
public void message()
{
System.out.println("书名:"+this.getBook());
System.out.println("作者:"+this.getWriter());
System.out.println("出版社:"+this.getPublish());
System.out.println("价格:"+this.price+"元");
}
}public class BookTest {
// 测试方法
public static void main(String[] args) {
//实例化对象,调用相关方法实现运行效果
Book one=new Book("红楼梦","曹雪芹","人民文学出版社",1);
one.message();
System.out.println("============================");
Book two=new Book("小李飞刀","古龙","中国长安出版社",55.5);
two.message();
}
}
sa1tedFish
2018-01-30 12:36:49
emmm打错了是练习
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星