Java封装练习1-9, 烦请老师检查和指正~
package imooc_object_books; public class Book { private String name; private String author; private String press; private double price; public Book() { } public Book(String name, String author, String press, double price) { this.name = name; this.author = author; this.press = press; this.setPrice(price); } public String getName() { return name; } public String getAuthor() { return author; } public String getPress() { return press; } public void setPress(String press) { this.press = press; } public double getPrice() { return price; } public void setPrice(double price) { if(price <= 10) { this.price = 10; System.out.println("The lowest price of the book is ¥10!"); }else { this.price = price; } } public String intro() { String str = "Name: " + this.getName() + ", Author: " + this.getAuthor() + ", Press: " + this.getPress() + ", Price: " + this.getPrice(); return str; } } package imooc_object_books; public class BookTest { public static void main(String[] args) { Book bookOne = new Book("Hongloumeng", "Caoxueqin", "Renminwenxue", 9.0); Book bookTwo = new Book("Xiaolifeidao", "Gulong", "Zhongguochangan", 55.5); System.out.println(bookOne.intro()); System.out.println("===================================================="); System.out.println(bookTwo.intro()); } }
77
收起
正在回答
1回答
已完成练习,代码实现符合题目要求,很好,继续加油!
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星