class Demo3_Phone {
public static void main(String[] args) {
Phone ph=new Phone(); //创建对象
ph.setBrand("apple");
ph.setPrice(6800);
System.out.println("brand="+ph.getBrand()+" "+"price="+ph.getPrice());
ph.show(); //仅仅是显示(再用brand或者price时就无法再调用)
System.out.println("-----------------------------");
Phone ph1=new Phone("三星",5000);
ph1.show();
ph.call();
ph.sendMessage();
ph.playGame();
}
}
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);
}
public void call(){ //类中特有方法
System.out.println("打电话");
}
public void sendMessage(){
System.out.println("发信息");
}
public void playGame(){
System.out.println("打游戏");
}
}
|
|