声明一个图书类,其数据成员为书名、编号(利用静态变量实现自动编号)、书价,并拥有静态数据成员册数,记录图书的总册数;在构造方法中,利用静态变量为对象的编号赋值,在主方法中定义对象数组,并求出总册数。java
时间: 2024-05-07 17:16:45 浏览: 98
好的,您可以问我问题了。
对于您的要求,我理解了。下面是您所需的图书类的Java代码:
```java
public class Book {
private String bookName;
private int bookId;
private double bookPrice;
private static int totalBooks = 0;
public Book(String name, double price) {
this.bookName = name;
this.bookPrice = price;
this.bookId = ++totalBooks;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public double getBookPrice() {
return bookPrice;
}
public void setBookPrice(double bookPrice) {
this.bookPrice = bookPrice;
}
public static int getTotalBooks() {
return totalBooks;
}
}
```
在主方法中,您可以按照下面的方式定义对象数组,求出总册数:
```java
public static void main(String[] args) {
Book[] books = new Book[10];
books[0] = new Book("Java编程思想", 45.9);
books[1] = new Book("Java核心技术", 68.0);
books[2] = new Book("Java程序设计", 29.9);
// ... 其他书籍的初始化
System.out.println("总共有 " + Book.getTotalBooks() + " 本书。");
}
```
以上就是您所需的Java代码了,希望对您有帮助!
阅读全文