正在回答
您好,您的代码在在线编译器也是可以运行的,帮您验证了一下。但是您的代码和在线编译器的类名是不匹配的,所以在运行之前需要修改一下类名,才能正常提交运行。您的代码可以进行优化,比如Book类中添加构造方法,进行相关属性的初始化赋值,以及相关信息的输出可以添加一个info()方法,放在info()方法中。
public class Book {
//私有属性:书名、作者、出版社、价格
private String bookName;
private String author;
private String press;
private double price;
//通过构造方法实现属性赋值
Book(String author,String bookName)
{
this.author = author;
this.bookName = bookName;
}
/*通过公有的get/set方法实现属性的访问,其中:
1、限定图书价格必须大于10,如果无效需进行提示,并强制赋值为10
2、限定作者、书名均为只读属性
*/
public String getBookName(){
return this.bookName;
}
public String getAuthor(){
return this.author;
}
public String getPress(){
return this.press;
}
public void setPress(String press){
this.press = press;
}
public double getPrice(){
return this.price;
}
public void setPrice(double price){
if(price>10){
this.price = price;
}else{
System.out.println("图书价格低于10元");
this.price = 10.0;
}
}
//信息介绍方法,描述图书所有信息
public void information(){
System.out.println("书名: "+this.getBookName());
System.out.println("作者: "+this.getAuthor());
System.out.println("出版社: "+this.getPress());
System.out.println("价格: "+this.getPrice()+"元");
}
}
public class BookTest {
// 测试方法
public static void main(String[] args) {
//实例化对象,调用相关方法实现运行效果
Book book = new Book("曹雪芹","红楼梦");
book.setPress("人民文学出版社");
book.setPrice(10.0);
book.information();
System.out.println("=====================================");
Book book2 = new Book("古龙","小李飞刀");
book2.setPress("中国长安出版社");
book2.setPrice(55.5);
book2.information();
}
}public class book {
//私有化属性
private String bookName1="红楼梦";//书名
private String author1="曹雪芹";//作者
private String bookName2="小李飞刀";//书名
private String author2="古龙";//作者
private String publishingHouse;//出版社
private double price;//价格
//书名
public String getBookName1(){
return "书名:"+bookName1;
}
public String getBookName2(){
return "书名:"+bookName2;
}
//作者
public String getAuthor1() {
return"作者:"+ author1;
}
public String getAuthor2() {
return"作者:"+ author2;
}
//出版社
public String getPublishingHouse() {
return "出版社:"+publishingHouse;
}
public void setPublishingHouse(String publishingHouse) {
this.publishingHouse = publishingHouse;
}
//价格
public double getPrice() {
return price;
}
public void setPrice(double price) {
if(price<10.0){
price=10.0;
System.out.println("图书价格不得低于10.0元!");
}
this.price = price;
}
public static void main(String[] args) {
book one=new book();
one.setPublishingHouse("人民文学出版社");
one.setPrice(1.0);
book tow=new book();
tow.setPublishingHouse("中国长安出版社");
tow.setPrice(55.5);
System.out.println(tow.getBookName1());
System.out.println(one.getAuthor1());
System.out.println(one.getPublishingHouse());
System.out.println("价格:"+one.getPrice()+"元");
System.out.println("=============================");
System.out.println(tow.getBookName2());
System.out.println(tow.getAuthor2());
System.out.println(tow.getPublishingHouse());
System.out.println("价格:"+tow.getPrice()+"元");
}
复制过程没出错
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星