请老师指点还有什么地方可以改进?
package book;
public class Book {
//私有属性:书名、作者、出版社、价格
private String name,author,publisher;
private double price;
//通过构造方法实现属性赋值
public Book(String name,String author,String publisher,double price){
this.name=name;
this.author=author;
this.setPublisher(publisher);
this.setPrice(price);
}
public String getName(){
return "书名:"+name;
}
public String getAuthor(){
return "作者:"+author;
}
public void setPublisher(String publisher){
this.publisher=publisher;
}
public String getPublisher(){
return "出版社:"+publisher;
}
public void setPrice(double price){
if(price<=10){
System.out.println("你输入了无效价格,自动将价格调至10");
this.price=10;
}else this.price=price;
}
public String getPrice(){
return "价格:"+price+"元";
}
/*通过公有的get/set方法实现属性的访问,其中:
1、限定图书价格必须大于10,如果无效需进行提示,并强制赋值为10
2、限定作者、书名均为只读属性
*/
//信息介绍方法,描述图书所有信息
}
对象:
package book;
public class BookTest {
public static void main(String[] args) {
Book one =new Book("红楼梦","曹雪芹","人民文学出版社",5);
System.out.println(one.getName());
System.out.println(one.getAuthor());
System.out.println(one.getPublisher());
System.out.println(one.getPrice());
System.out.println("==============================");
Book two =new Book("小李飞刀","古龙","中国长安出版社",55.5);
System.out.println(two.getName());
System.out.println(two.getAuthor());
System.out.println(two.getPublisher());
System.out.println(two.getPrice());
}
}
正在回答
同学的代码功能完成的不错,但是建议同学将描述图书所有信息的代码写在一个方法中,这样在测试类中只需调用这个方法,就能展示图书的所有信息,减少冗余的代码。祝学习愉快。
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星