编程练习打卡,请老师检查

来源:1-9 编程练习

Mycheol

2020-07-22 16:24:23

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

  //信息介绍方法,描述图书所有信息
    public void info(){
        System.out.println("书名:" + this.getTitle());
        System.out.println("作者:" + this.getAuthor());
        System.out.println("出版社:" + this.getPress());
        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();
        System.out.println("===============================");
        b2.info();
    }
}


写回答

1回答

好帮手慕阿园

2020-07-22

同学你好,练习题完成的不错,很棒,继续加油

祝学习愉快~


0

0 学习 · 16556 问题

查看课程