编程打卡,请老师检查~

来源:1-9 编程练习

阿姜爱学习

2020-11-23 17:57:33

# 具体遇到的问题
请问老师,我这种写法更好还是把显示信息的函数写在类内好呢,

是否根据单一职责原则,这类业务函数不应写在类内?

Book.java

/**
* 标题:
*
* @Author:
* @Date: 2020/11/23 17:24
*/
public class Book {
private String bookname;
   private String writer;
   private String faxingshe;
   private double prize;

   public Book() {

}
public Book(String bookname, String writer, String faxingshe, double prize) {
this.bookname = bookname;
       this.writer = writer;
       this.setFaxingshe(faxingshe);
       this.setPrize(prize);
   }

public String getBookname() {
return this.bookname;
   }

public String getWriter() {
return this.writer;
   }

public String getFaxingshe() {
return this.faxingshe;
   }

public void setFaxingshe(String faxingshe) {
this.faxingshe = faxingshe;
   }

public double getPrize() {
return this.prize;
   }

public void setPrize(double prize) {
if (prize<=10.0) {
System.out.println("图书价格最低10");
           this.prize = 10.0;
       }else {
this.prize = prize;
       }
}

}

BookTest.java


/**
* 标题: 测试类
*
* @Author:
* @Date: 2020/11/23 17:24
*/
public class BookTest {
public static void introducebook(Book book) {
System.out.println("书名:" + book.getBookname());
       System.out.println("作者:" + book.getWriter());
       System.out.println("出版社:" + book.getFaxingshe());
       System.out.println("价格:" + book.getPrize() + "");
   }

public static void main(String[] args) {
Book b1 = new Book("红楼梦","曹雪芹","人民文学出版社",10);
       Book b2 = new Book("小李飞刀","古龙","中国长安出版社",55.5);
       introducebook(b1);
       System.out.println("=========================================================");
       introducebook(b2);
   }
}


写回答

1回答

好帮手慕阿慧

2020-11-23

同学你好,显示信息的函数是展示图书信息,写在Book类里面好。

参考代码如下:

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

0

0 学习 · 16556 问题

查看课程