class Demo5_Phone {
public static void main(String[] args) {
Phone p = new Phone("三星",2800); //创建对象
p.show(); //调用成员方法
Phone p1 = new Phone();
p1.setBrand("苹果");
p1.setPrice(6800);
System.out.println(p1.getBrand() +"手机只要"+ p1.getPrice()+"元"+",千万别卖肾!~");
}
}
class Phone
{
private String brand; //私有品牌
private int price; //私有价格
public Phone() {} //无参构造方法
public Phone(String brand,int price ) //有参构造方法
{
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() //定义show方法
{
System.out.println(brand +"手机只要"+ price+"元");
}
}
|
|