class Phone {
//品牌
private String brand;
//价格
private int price;
//颜色
private String color;
public Phone() {}
public Phone(String brand,int price,String color) {
this.brand = brand;
this.price = price;
this.color = color;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
//输出所有成员变量
public void show() {
System.out.println(brand+"---"+price+"---"+color);
}
}
//测试
class Work {
public static void main(String[] args) {
//方法1,输出成员变量
//无参+getXxx()
Phone p = new Phone();
p.setBrand("华为");
p.setPrice(2999);
p.setColor("黑色");
System.out.println(p.getBrand()+"---"+p.getPrice()+"---"+p.getColor());
p.show();
System.out.println("-----------");
//方法1,输出成员变量
//有参
Phone p2 = new Phone("华为",2999,"黑色");
System.out.println(p2.getBrand()+"---"+p2.getPrice()+"---"+p2.getColor());
p2.show();
}
} |
|