老师帮忙看看,为什么报错了。

来源:1-7 编程练习

Lucky2295237

2020-02-27 12:41:27

public class Book {

  //私有属性:书名、作者、出版社、价格

   private String title;//书名

   private String author;//作者

   private String press;//出版社

   private float price;//价格

  //通过构造方法实现属性赋值

 public Book(String title,String author,String press, float price){

   this.title=title;

   this.author=author;

   this.press=press;

   this.setPrice(price);


}

    /*通过公有的get/set方法实现属性的访问,其中:

    1、限定图书价格必须大于10,如果无效需进行提示,并强制赋值为10

    public void setPrice(float price){

    if(price<10f){

    System.out.println("图书价格最低10元");

     price=10f;

   } else{

      this.price=price;

    }

    }

    public float getPrice(){

        return price;

    }

    2、限定作者、书名均为只读属性

    */

     public String getauthor(){

        return author;

     }

     public String gettitle(){

        return title;

     }

      public String getpress() {

        return  press;

      }

  //信息介绍方法,描述图书所有信息

  public void Abook(){

        System.out.println("书名:"+title);

        System.out.println("作者:"+author);

        System.out.println("出版社:"+press);

        System.out.println("价格:"+ price+"元");


}

}

http://img.mukewang.com/climg/5e574857094dda4903050171.jpg

写回答

1回答

好帮手慕小脸

2020-02-27

同学你好,错误原因是因为没有找到price的set()方法,同学把注释掉的set()方法取消掉注释即可。具体修改如下:

http://img.mukewang.com/climg/5e575bcf0910b98907380478.jpg

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

0

0 学习 · 11489 问题

查看课程