请问老师我的代码有何地方可以优化?

请问老师我的代码有何地方可以优化?

-----------------BookTest.java部分---------------------------------------------------------------------------

public class BookTest {


     // 测试方法

public static void main(String[] args) {

      //实例化对象,调用相关方法实现运行效果

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

        Book bk=new Book();

        bk.book("红楼梦","曹雪芹","人民文学出版社",10);

        System.out.println("====================================");

        bk.book("小李飞刀","古龙","中国长安出版社",55.5);

     }

}



------------------Book.java部分-------------------------------------------------------------------------------

public class Book {

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

     private String name;

     private String author;

     private String company;

     private double price;

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

    public String getName(){

        return "书名:"+this.name;

    }

    public String getAuthor(){

        return "作者:"+this.author;

    }

    public String getCompany(){

        return "出版社:"+this.company;

    }

    public void setPrice(double price){

        if(price<=10)this.price=10;

        else this.price=price;

    }

    public double getPrice(){

        return this.price;

    }

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

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

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

    */


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

    public void book(String name,String author,String company,double price){

        this.name=name;

        this.author=author;

        this.company=company;

        this.price=price;

        System.out.println(getName());

        System.out.println(getAuthor());

        System.out.println(getCompany());

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

    }


}


正在回答

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

1回答

上述代码有如下问题:

1)构造方法里只完成属性赋值,更多的业务处理移到其他方法中,比如输出语句等业务逻辑;

2)题目要求“限定图书价格必须大于10,如果无效需进行提示”,所以在setPrice()方法中当if (price <= 10)给出提示信息

3)题目要求,作者、书名为只读,所以出版社需要有set属性

4)添加描述图书所有信息的介绍方法

  • qq_Ken_cklBKS 提问者 #1
    -----------BOOK.java------- public class Book { private String name; private String author; private String company; private double price; public Book(String name,String author,String company,double price){ this.name=name; this.author=author; setCompany(company); setPrice(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 String getName(){ return this.name; } public String getAuthor(){ return this.author; } public String getCompany(){ return this.company; } public double getPrice(){ return this.price; } public void information(){ System.out.println("书名:"+getName()); System.out.println("作者:"+getAuthor()); System.out.println("出版社:"+getCompany()); System.out.println("价格:"+getPrice()+"元"); } } -------BOOKTEST.java------ public class BookTest { public static void main(String[] args) { Book one=new Book("红楼梦","曹雪芹","人民文学出版社",5); one.information(); System.out.println("====================="); Book two=new Book("小李飞刀","古龙","中国长安出版社",55.5); two.information(); } }
    2019-02-20 13:33:38
  • qq_Ken_cklBKS 提问者 #2
    老师这样呢?长度过大,我只能把注释删了
    2019-02-20 13:34:04
  • 嗯嗯 可以 这样做更符合题目要求~
    2019-02-20 13:36:59
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

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