//:手机类的案例 成语变量:品牌,价格 成语方法:
class Demo3_Phone {
public static void main(String[] args) {
Phone p=new Phone();
p.setBrand("三星");
p.setPrice(5678);
System.out.println("手机的品牌是:"+p.getBrand()+",价格是:"+p.getPrice());
Phone p1=new Phone("苹果",7890);
p1.show();
}
}
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(){
System.out.println("手机的品牌是:"+brand+"价格是"+price);
}
}
|
|