JAVA object-oriented case analysis (3)

JAVA case analysis (2)

Object-oriented
Declare a book class, its data members are book name, serial number (using static variables to realize automatic numbering), book price, and have the number of static data members, record the total number of books, use this static variable as the object in the construction method Number assignment, define multiple objects in the main method, and find the total number of books.

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;//先赋值在进行count自增
		this.title=title;
		this.price=price;
		count++;
	}
	//setter、getter略
	public String getInfo(){
    
    
		return "图书编号:"+this.bid+"、名称:"+this.title+"、价格:"+this.price;
	}
	public static int getCount(){
    
    
		return count;
	}
}
class JavaDemo{
    
    
	public static void main(String[] args) {
    
    
		Book b1=new Book("JAVA",99.2);
		Book b2=new Book("ORACLE",89.2);
		System.out.println(b1.getInfo());
		System.out.println(b2.getInfo());
		System.out.println("图书总册数:"+Book.getCount());
	}
} 

In the most basic object-oriented development, simple Java classes are the best solution to pre-design.

Guess you like

Origin blog.csdn.net/weixin_46688667/article/details/107825434