代码为什么报错???

代码为什么报错???

public class Book {
  //私有属性:书名、作者、出版社、价格
    private String book;
    private String author;
    private String company;
    private double price;
  //通过构造方法实现属性赋值
 // public Book(){
  //}
  public Book(String book,String author,String company,double price){ 
         this.book=book;
         this.author=author;
         this.company=company;
         this.price=price;
  }
    /*通过公有的get/set方法实现属性的访问,其中:
    1、限定图书价格必须大于10,如果无效需进行提示,并强制赋值为10
    2、限定作者、书名均为只读属性
    */
   public String getBook(){
      return this.book;
   }
   public String getAuthor(){
      return this.author;
   }
   public String getCompany(){
       return this.company;
   }
   public double getPrice(){
       return this.price;
   }
   public void setCompany(String Company){
       this.company=company;
   }
   public void setPrice(double price){
       if(price<=10){
           System.out.println("图书价格最低10元");
          this.price=10;
       }else{
       this.price=price;
       }
  //信息介绍方法,描述图书所有信息
  public void info(){
      System.out.println("书名:"+this.getBook);
      System.out.println("作者:"+this.getAuthor);
      System.out.println("出版社"+this.getCompany);
      System.out.println("价格:"+this.getPrice+"元");
  }
}



public class BookTest {

     // 测试方法
	 public static void main(String[] args) {
      //实例化对象,调用相关方法实现运行效果
     Book b1=new Book("红楼梦","曹雪芹","人民文学出版社",10);
     Book b2=new Book("小李飞刀","古龙","中国长安出版社",55.5);
     
     b1.info();
     b2.info();
     }
}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
irista23 2018-08-13 16:42:20

这里需要调用setPrice()方法,直接赋值没有用到setPrice()就不会输出信息

http://img1.sycdn.imooc.com//climg/5b71444b0001a3a105210107.jpg

  • 提问者 慕羲 #1
    更换为 getPrice(price);
    2018-08-13 17:32:31
  • irista23 回复 提问者 慕羲 #2
    get是取值,set是赋值,你要做的是赋值操作
    2018-08-13 17:48:57
irista23 2018-08-13 11:33:39

1、下图中的位置少一个结束大括号

http://img1.sycdn.imooc.com//climg/5b70fbb20001a7f504000155.jpg

2、下图中红框部分调用的都是方法,应该加小括号

http://img1.sycdn.imooc.com//climg/5b70fbee00013e0304490145.jpg

System.out.println("书名:" + this.getBook());
System.out.println("作者:" + this.getAuthor());
System.out.println("出版社" + this.getCompany());
System.out.println("价格:" + this.getPrice() + "元");


  • 提问者 慕羲 #1
    为什么红楼梦10元,不会打“图书价格最低10元”这句话,哪里报错???
    2018-08-13 16:38:03
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师