在项目中创建Book类,在类中定义3个成员变量,分别表示书名,作者与价格,同时提供构造方法和成员方法来修改成员变量。
package ace;
public class Book {
private String title;
private String author;
private double price;
public Book(String title,String author,double price) {
this.title=title;
this.author=author;
this.price=price;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public double getPrice() {
return price;
}
}
再创建一个测试类Test
package ace;
public class Test {
public static void main(String[] args) {
Book book=new Book("《小王子》","abc",34.6);
System.out.println("书名:"+book.getTitle());
System.out.println("作者:"+book.getAuthor());
System.out.println("价格:"+book.getPrice()+"元");
}
}
运行结果如下