用java创建一个类Book,在Book中有String类型的属性name,author,有double类型的属性price,定构造方法带三个参数,给三个属性赋值。定义方法printBookInfo()输出三个属性的值。 创建测试类TestBook,生成Book对象b1,调用printBookInfo()方法
public class Book {
private String name;
private String author;
private double price;
public Book(String name, String author, double price) {
this.name = name;
this.author = author;
this.price = price;
}
public void printBookInfo() {
System.out.println("书名:" + name);
System.out.println("作者:" + author);
System.out.println("价格:" + price);
}
}
public class TestBook {
public static void main(String[] args) {
Book b1 = new Book("大话西游", "周星驰", 99.9);
b1.printBookInfo();
}
}