声明一个图书类,其数据成员为书名、编号(利用静态变量实现自动编号)、书价,并拥有静态数据成员册数,记录图书的总册数;在构造方法中,利用静态变量为对象的编号赋值,在主方法中定义对象数组,并求出总册数。。java代码
时间: 2024-06-01 22:12:07 浏览: 62
public class Book {
private String name;
private int id;
private double price;
private static int totalCount = 0;
public Book(String name, double price) {
this.name = name;
this.price = price;
this.id = ++totalCount;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public double getPrice() {
return price;
}
public static int getTotalCount() {
return totalCount;
}
}
public class Main {
public static void main(String[] args) {
Book[] books = new Book[3];
books[0] = new Book("Java Programming", 59.99);
books[1] = new Book("Data Structures and Algorithms", 79.99);
books[2] = new Book("Database Management", 69.99);
System.out.println("Total book count: " + Book.getTotalCount());
}
}