请问下我的代码正确吗
来源:1-7 编程练习
黄酒黄酒黄酒
2020-03-17 02:02:58
public class Book {
//私有属性:书名、作者、出版社、价格
private String bookTitle;
private String author;
private String publishingHouse;
private double price;
//通过构造方法实现属性赋值
/**
* 书名只读
* @return
*/
public String getBookTitle() {
return bookTitle;
}
/**
* 作者只读
* @return
*/
public String getAuthor() {
return author;
}
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) {
System.out.println("图书价格最低10元");
this.price = 10;
} else {
this.price = price;
}
}
public Book(String bookTitle,String author){
this.bookTitle=bookTitle;
this.author=author;
}
public void string(){
System.out.println("书名:"+bookTitle);
System.out.println("作者:"+author);
System.out.println("出版社:"+publishingHouse);
System.out.println("价格:"+price+"元");
}
public void dividingLine(){
System.out.println("================================");
}
/*通过公有的get/set方法实现属性的访问,其中:
1、限定图书价格必须大于10,如果无效需进行提示,并强制赋值为10
2、限定作者、书名均为只读属性
*/
//信息介绍方法,描述图书所有信息
public class BookTest { //测试方法 public static void main(String[] args) { //实例化对象,调用相关方法实现运行效果 Book we=new Book("红楼梦","曹雪芹"); we.setPublishingHouse("人民文学出版社"); we.setPrice(10.0); we.string(); we.dividingLine(); Book we1=new Book("小李飞刀","曹雪芹"); we1.setPublishingHouse("中国长安出版社"); we1.setPrice(55.5); we1.string(); } }
1回答
同学你好,是正确的,继续加油!
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
相似问题