1-7作业检查

来源:1-7 编程练习

aming41

2020-05-17 17:31:44

public class Book {

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

    private String bookName;

    private String author;

    private String press;

    private double price;

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

    public Book(String bookName, String author){

        this.bookName = bookName;

        this.author = author;

//         this.press = press;

//         this.setPrice(price);

        

    }

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

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

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

    */

    public String getBookName(){

        return this.bookName;

    }

    

    public String getAuthor(){

        return this.author;

    }

    

    public void setPrice(double price){

        if(price < 10){

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

            price = 10;

        }

        this.price = price;

    }

    

    public double getPrice(){

        return this.price;

    }

    

    public void setPress(String press){

        this.press = press;

    }

    

    public String getPress(){

        return this.press;

    }

    

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

    public void bookIntro(){

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

        "\n作者:" + this.getAuthor() + 

        "\n出版社:" + this.getPress() +

        "\n价格:" + this.getPrice());

    }


}


public class BookTest {


public static void main(String[] args) {

Book b = new Book("红楼梦", "曹雪芹");

Book b2 = new Book("小李飞刀", "古龙");

b.setPress("人民文学出版社");

b.setPrice(9);

b.bookIntro();

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

b2.setPress("中国长安出版社");

b2.setPrice(55.5);

b2.bookIntro();

}


}


写回答

1回答

好帮手慕小脸

2020-05-17

同学你好,思路正确,写的很棒哦~

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

0

0 学习 · 11489 问题

查看课程