Topic analysis
With the idea of object-oriented, write a self defining class to describe the book information. The set attributes include: book name, author, publisher name, price; set the private access rights of the attribute, and access to the attribute through the public get and set methods] (here write the custom directory title)
task
Property: title, author, publishing house, price
Method: information introduction
Requirement:
1. Design constructors to realize attribute assignment
2. Set private property and get/set method to access property
3. The price of the limited book must be greater than 10. If it is invalid, a prompt shall be given and the forced value shall be 10
4. Limit the author and title to read-only
5. Information introduction method describes all information of the book
The code is as follows:
public class Book { // Private property: title, author, publishing house, price private String bookname; private String zzname; private String cbs; private double jg; // Property assignment by construction method public Book() { } public Book(String bookname, String zzname, String cbs, double jg) { this.bookname = bookname; this.zzname = zzname; this.cbs = cbs; this.jg = jg; } /* * The public get/set method is used to access the properties, where: * 1,The price of the limited book must be greater than 10. If it is invalid, you need to prompt and force the value to 10 * 2,Restrict the author and title to read-only */ public String getCbs() { return cbs; } public void setCbs(String cbs) { this.cbs = cbs; } public double getJg() { return jg; } public void setJg(double jg) { if (jg > 10) { this.jg = jg; } else { System.out.println("The lowest price of books is 10 yuan"); this.jg = 10; } } public String getBookname() { return bookname; } public String getZzname() { return zzname; } public void setBookname(String bookname) { this.bookname = bookname; } public void setZzname(String zzname) { this.zzname = zzname; } //Information introduction method, describing all information of the book public void showInfo() { System.out.println("Title:" + this.getBookname()); System.out.println("author:" + this.getZzname()); System.out.println("Press::" +cbs); System.out.println("Price:" + jg+"element"); } }
public class BookTest { // test method public static void main(String[] args) { //Instantiate the object and call related methods to achieve the running effect Book n =new Book("The Dream of Red Mansion", "Cao Xueqin", "People's Literature Press", 10.0); n.showInfo(); System.out.println("=============="); Book m =new Book("knife man", "Gulong", "China Chang'an press", 55.5); m.showInfo(); } }