老师我不知道代码哪里有错误,我弄不成图片那样子~代码如下~
来源:1-9 编程练习
Heijyu
2020-05-18 18:23:42
package com.imooc.exercise1_9;
public class Book {
// 私有属性:书名、作者、出版社、价格
private String name,author,press;
private double price;
// 通过构造方法实现属性赋值
public Book() {
}
public Book(String name, String author) {
this.getBookInformation();
this.name = name;
this.author = author;
}
/*
* 通过公有的get/set方法实现属性的访问,其中: 1、限定图书价格必须大于10,如果无效需进行提示,并强制赋值为10 2、限定作者、书名均为只读属性
*/
public String getName() {
System.out.println("");
return name;
}
public String getAuthor() {
System.out.println("");
return author;
}
public String getPress() {
System.out.println("");
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("图书价格最低为10元!");
}
else {
this.price = price;
System.out.println("价格:" + price + "元!");
}
}
// 信息介绍方法,描述图书所有信息
public String getBookInformation() {
String str1 = "书名:" + name;
String str2 = "作者:" + author;
String str3 = "出版社:" + press;
String str4 = "价格:" + price;
return str1 + "\n" + str2 + "\n" + str3 + "\n" +str4;
}
}
package com.imooc.exercise1_9;
public class BookTest {
public static void main(String[] args) {
// 实例化对象,调用相关方法实现运行效果
Book book = new Book("红楼梦", "曹雪芹");
book.setPress("人民文学出版社");
System.out.println(book.getBookInformation());
book.setPrice(0);
System.out.println(book.getPrice());
System.out.println("=======================================");
book = new Book("小李飞刀","曹雪芹");
book.setPress("中国长安出版社");
book.setPrice(55.5);
System.out.println(book.getBookInformation());
}
}效果图对比如下:
价格10.0和0.0会出现在那里弄不掉~
4回答

好帮手慕小脸
2020-05-18
同学你好,“橘七”同学说的方法是可以的,同学可以参考一下
祝学习愉快~
好帮手慕小尤
2020-05-18
同学你好 ,橘七同学回答是正确的,同学可以进行参考。
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
KLovei
2020-05-18

你把这两个句代码的位置换一下
相似问题