麻烦老师检查,简化下代码,麻烦老师了

来源:1-7 编程练习

伽文Sama

2020-02-05 17:09:08

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;

    this.setPrice(price);

      this.setCompany(company);

  }

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

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

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

    */

    public String getAuthor(){

        return author;

    }

    public String getName(){

        return name;

    }

    public void setPrice(double price){

        if(price<10.0){

            System.out.println("图书价格必须大于10");

            price=10;

        }else{

            this.price=price;

        }

    }

    public String getPrice(){

        return this.price+"元";

    }

    public void setCompany(String company){

        this.company=company;

    }

    public String getCompany(){

        return this.company;

    }

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

    public void id(){

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

        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 one=new Book("红楼梦","曹雪芹","人民文学出版社",10.0);

  one.id();

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

   Book two=new Book("小李飞刀","古龙","中国长安出版社",55.5);

two.id(); 

}

    

}


写回答

1回答

好帮手慕柯南

2020-02-05

同学你好!

代码整体完成不错,但是有一个小问题,你以下功能设置失败了

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

应该是this.price=10;

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

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

0

0 学习 · 11489 问题

查看课程