1.以面向对象的思想,编写自定义类描述图书信息。设定属性包括:书名,作者,出版社名,价格;方法包括:信息介绍
2.要求:
- 设置属性的私有访问权限,通过公有的get,set方法实现对属性的访问
- 限定介格必须大于10,如果无效进行提示
- 设计构造方法实现对属性赋值
- 信息介绍方法描述图书所有信息
- 编写测试类,测试图书类的对象及相关方法(测试数据信息自定)
3.源码
public class Book {
private String bookname;
private String author;
private String publish;
private double price;
public void setPrice(double price) {
if (price < 10) {
System.out.println("价格无效,已修改默认值");
this.price = 10.0;
} else {
this.price = price;
}
}
public Book(String bookname, String author, String publish, double price) {
this.bookname = bookname;
this.author = author;
this.publish = publish;
this.setPrice(price);
}
/**
* 显示图书信息
*/
public void introduce() {
System.out.println("书名:" + bookname);
System.out.println("作者:" + author);
System.out.println("出版社:" + publish);
System.out.println("价格:" + price + "元");
}
}
测试类
public class BookTest {
public static void main(String[] args) {
Book book=new Book("鹿鼎记","金庸","人民文学出版社",9.0);
book.introduce();
}
}