麻烦老师看一下下面这段代码的问题,请问如何限定作者、书名均为只读属性???
来源:1-7 编程练习
慕标3002004
2020-03-04 11:23:59
public class Book {
//私有属性:书名、作者、出版社、价格
private String bookName;
private String author;
private String publish;
private double price;
//通过构造方法实现属性赋值
public Book(String bookName,String author,String publish,double price){
setprice(price);
setpublish(publish);
this.bookName=bookName;
this.author=author;
}
/*通过公有的get/set方法实现属性的访问,其中:
1、限定图书价格必须大于10,如果无效需进行提示,并强制赋值为10
2、限定作者、书名均为只读属性
*/
public String getbookName(){
return bookName;
}
public String getauthor(){
return author;
}
public String getpublish(){
return publish;
}
public viod setpublish(String publish){
this.publish=publish;
}
public double getprice(){
return price;
}
public viod setprice(double price){
if(price<=10){
System.out.prinlt("图书价格最低10元");
this.price=10;}
else{
this.price=price;}
}
//信息介绍方法,描述图书所有信息
}
1回答
同学你好,
1、请问如何限定作者、书名均为只读属性???
作者、书名均为只读属性,也就是作者和书名不能有set方法,只能有get方法
2、代码中其他问题如下:
1)void,不是viod,修改如下:
2)打印语句是println不是prinlt
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
相似问题