/*手机类:成员变量:品牌brand 价格price 构造方法:无参,带两个参成员方法:getXxx()/setXxx()show():输出该类的所有成员变量值*/class Test705 { public static void main(String[] args) { Phone p1 = new Phone(); //使用空参 p1.setBrand("苹果"); p1.setPrice(5288); System.out.println(p1.getBrand() + p1.getPrice()); Phone p2 = new Phone("小米",588); //使用有参 p2.show(); }}class Phone { private String brand; private int price; public Phone(){ //空参 } public Phone(String brand,int pirce) { //有参 this.brand = brand; this.price = price; } public void setBrand(String brand) { this.brand = brand; } public String getBrand() { return brand; } public void setPrice(int price) { //设置价格 this.price = price; } public int getPrice() { //获取价格 return price; } public void show() { System.out.println(brand + ".." + price); }}
添加回答
举报
0/150
提交
取消