请检查代码,谢谢!
来源:1-7 编程练习
慕少6083615
2020-06-23 02:05:14
package com.imooc.getset; public class Book { // 私有属性:书名、作者、出版社、价格 private String title; private String author; private String press; private double price; // 通过构造方法实现属性赋值 public Book() { } public Book(String title, String author, String press, double price) { this.title=title; this.author=author; this.setPress(press); this.setPrice(price); } /* * 通过公有的get/set方法实现属性的访问,其中: * 1、限定图书价格必须大于10, 如果无效需进行提示,并强制赋值为10 * 2、限定作者、书名均为只读属性 */ public String getTitle() { return title; } public String getAuthor() { return author; } public void setPress(String press) { this.press = press; } public String getPress() { return press; } public void setPrice(double price) { if (price <= 10) { System.out.println("图书价格最低10元"); this.price = 10; } else { this.price = price; } } public double getPrice() { return price; } // 信息介绍方法,描述图书所有信息 public String intro() { String str = "书名:" + this.getTitle() + '\n' + "作者:" + this.getAuthor() + '\n' + "出版社:"+this.getPress()+'\n' +"价格:"+this.getPrice()+"元\n"; return str; } } package com.imooc.getset; public class BookTest { public static void main(String[] args) { //实例化对象,调用相关方法实现运行效果 Book book1=new Book("红楼梦","曹雪芹","人民文学出版社",1d); System.out.println(book1.intro()); System.out.println("=========================="); Book book2=new Book("小李飞刀","古龙","中国长安出版社",55.5d); System.out.println(book2.intro()); } }
1回答
已完成练习。棒棒哒!继续加油!
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
相似问题