public class Main{
public static void main(String[] args) {
Book b1=new Book("Java程序设计",34.5);
Book b2=new Book("数据结构",44.8);
Book b3=new Book("C++程序设计",35.0);
System.out.println(b1.getInfo());
System.out.println(b2.getInfo());
System.out.println(b3.getInfo());
System.out.print("图书总册数为:"+Book.getCount());
}
}
class Book{
private int bid;
private String title;
private double price;
private static int count=0;
public Book(String title,double price)
{
this.bid=count+1;
this.title=title;
this.price=price;
count++;
}
public String getInfo(){
return "书名:"+this.title+", 书号:"+this.bid+", 书价:"+this.price;
}
public static int getCount(){
return count;
}
}
声明一个图书类,其数据成员为书名、编号(利用静态变量实现自动编号)、书价,并拥有静态数据成员册数,记录图书的总册数;在构造方法中,利用静态变量为对象的编号赋值,在主方法中定义对象数组,并求出总册数。
输出格式:
请输出每本图书对应的书名,书号,书价以及总图书数。
输出样例:
书名:Java程序设计, 书号:1, 书价:34.5
书名:数据结构, 书号:2, 书价:44.8
书名:C++程序设计, 书号:3, 书价:35.0
图书总册数为:3