老师怎么优化

来源:1-7 编程练习

大大鱼pzy

2019-05-30 15:20:44

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


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


写回答

1回答

吃吃吃鱼的猫

2019-05-30

同学你好,写的很棒哦~

没有什么地方需要优化呢~

继续加油吧,祝:学习愉快~

0

0 学习 · 11489 问题

查看课程