首页 > 其他分享 >5.24 面向对象案例分析六

5.24 面向对象案例分析六

时间:2023-06-02 23:45:56浏览次数:49  
标签:String title price 案例 面向对象 Book private 5.24 public

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

标签:String,title,price,案例,面向对象,Book,private,5.24,public
From: https://www.cnblogs.com/pansidong/p/17453129.html

相关文章

  • 5.23 面向对象案例分析五
    用static,引入计数器案例classUser{privateStringuid;privateStringpassword;privatestaticintcount=0;publicUser(){this("NOID","mldn");}publicUser(Stringuid){this(uid,"mldnjava&q......
  • 5.21 面向对象案例分析三
    狗的一个类,包括名字,颜色,年龄;典型的java类的一段代码classDog{privateStringname;privateStringcolor;privateintage;publicDog(){}publicDog(Stringname,Stringcolor,intage){this.name=name;this.color=color;......
  • 5.20 面向对象案例分析二
    classEmployee{privatelongempno;privateStringename;privatedoublesalary;privatedoublerate;publicEmployee(){}publicEmployee(longempno,Stringename,doublesalary,doublerate){this.empno=empno;this......
  • 5.19 面向对象案例分析一
    classAddress{privateStringcountry;privateStringprovince;privateStringcity;privateStringstreet;privateStringzipcode;publicAddress(){}publicAddress(Stringcountry,Stringprovince,Stringcity,Stringstr......
  • C++面向对象
    文章目录C++类&对象C++类定义定义C++对象访问数据成员类&对象\详解类成员函数类访问修饰符构造函数&析构函数C++拷贝构造函数C++友元函数C++内联函数C++中的this指针C++中指向类的指针C++类的静态成员继承基类&派生类访问控制和集成继承类型多继承继承顺序重载运算符和重载函数C++......
  • Map系列集合:应用案例
     packagecom.Map;importjava.util.*;publicclassTest{publicstaticvoidmain(String[]args){//1.把80个学生选择的景点数据拿到程序中去List<String>data=newArrayList<>();String[]selects={"A","B",&quo......
  • 集合:综合案例:斗地主业务分析
     packagecom.GameDemo;publicclassGameDemo{publicstaticvoidmain(String[]args){Rooms=newRoom();//启动游戏s.start();}}packagecom.GameDemo;publicclassCard{privateStringnumber;privateStri......
  • 云原生第五周--k8s实战案例
    前言业务容器化优势:提高资源利用率、节约部署IT成本。提高部署效率,基于kubernetes实现微服务的快速部署与交付、容器的批量调度与秒级启动。实现横向扩容、灰度部署、回滚、链路追踪、服务治理等。可根据业务负载进行自动弹性伸缩。容器将环境和代码打包在镜像内,保证了测试......
  • 5.15 static 应用案例
    classBook{privateStringtitle;privatestaticintcount=0;publicBook(){//无参构造this("NOTITLE-"+count++);//避免了没有title的情况,调用下面的构造方法,把“NOTITLE-count”当作title}publicBook(Stringtitle){......
  • 5.14 static 应用案例
    demo1publicclassHelloWorld{publicstaticvoidmain(String[]args){print();//静态方法调用静态方法;}publicstaticvoidprint(){System.out.println("www.mldn.cn");}}lastdemopublicclassHelloWorld{publ......