代码哪里错了
1 | <br> |
public class Book {
//私有属性:书名、作者、出版社、价格
private String title;
private String author;
private String publishing;
private double price;
public Book(String title,String author,String publishing,double price){
this.title=title;
this.author=author;
this.setPublishing(publishing);
this.setPrice(price);
}
public String getPublishing() {
return publishing;
}
public void setPublishing(String publishing) {
this.publishing = publishing;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
if(price<=10){
System.out.println("图书价格最低10元");
this.price=10;
}else{
this.price=price;
}
}
public void setTitle(String title) {
this.title = title;
}
public void setAuthor(String author) {
this.author = author;
}
}
public class BookTest {
public static void main(String[] args) {
//实例化第一本书,四参构造方法
Book one=new Book("红楼梦","曹雪芹","人名文学出版社",10.0);
//实例化第二本书,四参构造方法
Book two = new Book("小李飞刀","古龙","中国长安出版社",55.5);
//第一第二本书描述信息方法
System.out.println("书名:"+one.setTitle(title));
System.out.println("作者:"+one.setAuthor(author));
System.out.println("出版社:"+one.getPublishing());
System.out.println("价格:"+one.getPrice());
System.out.println("===========================================");
System.out.println("书名:"+two.setTitle(title));
System.out.println("作者:"+two.setAuthor(author));
System.out.println("出版社:"+two.getPublishing());
System.out.println("价格:"+two.getPrice());
}
}
正在回答
Book类中缺少title和author的getter方法,增加getter方法之后,可以直接调用getter方法完成打印输出。即
System.out.println("书名:"+ one.getTitle());
System.out.println("作者:"+one.getAuthor());
two对象的同上。
建议设置好类中的属性之后,首先根据作业要求生成其getter和setter方法。
祝学习愉快~
- 参与学习 人
- 提交作业 5461 份
- 解答问题 7238 个
此次推出的专题为Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始学习Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星