5.24 面向对象案例分析六

class Book {// 类的名称要以class开头,否则报错,并且提示不到这行代码!!!
    private int bid;
    private String title;
    private double price;
    private static int count = 0;

    public Book(String title,double price){
        count++;
        this.bid = count;
        this.title = title;
        this.price = price;
    }

    // setter ,getter 略
    public String getInfo() {
        return "图书编号:" + this.bid + ",书名称:" + this.title + ",价格:" + this.price;
    }

    public static int getCount() {
        return count;
    }

}

public class HelloWorld {
    public static void main(String[] args){
        Book b1 = new Book("Java",10.0);
        Book b2 = new Book("Php",33.0);
        System.out.println(b1.getInfo());
        System.out.println(b2.getInfo());
        System.out.println("图书总册书:" + Book.getCount());
    }
   
}
  • 结果
图书编号:1,书名称:Java,价格:10.0
图书编号:2,书名称:Php,价格:33.0
图书总册书:2
posted @ 2023-06-02 23:26  盘思动  阅读(5)  评论(0编辑  收藏  举报