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回答
已完成练习,代码实现符合题目要求,很好,继续加油!
2023版Java工程师
- 参与学习 人
- 提交作业 8788 份
- 解答问题 9886 个
综合就业常年第一,编程排行常年霸榜,北上广深月薪过万! 不需要基础,无需脱产即可学习,只要你有梦想,想高薪! 全新升级:技术栈升级(包含VUE3.0,ES6,Git)+项目升级(前后端联调与功能升级)
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星