1. 程式人生 > >java寫一個類,包括資訊為手機的型號,價格和顏色。功能為打電話給某人和發信息給某人

java寫一個類,包括資訊為手機的型號,價格和顏色。功能為打電話給某人和發信息給某人

package henu.rjxy.demo01;
public class Demo01Phone {
private String brand;
private String price;
private String color;
private boolean online;
	public void Phone(String brand, String price, String color,boolean online) {
//	super();
	this.brand = brand;
	this.price = price;
	this.color = color;
	this.online = online;
	}
	public String getBrand() {
	return brand;
	}
	public void setBrand(String brand) {
	this.brand = brand;
	}
	
	public String getPrice() {
	return price;
	}
	public void setPrice(String price) {
	this.price = price;
	}
	public String getColor() {
	return color;
	}
	public void setColor(String color) {
	this.color = color;
	}
	public boolean isOnline() {
	return online;
	}
	public void setOnline(boolean online) {
	this.online = online;
	}
//1.打電話方法
	public void phoneCall(String phoneName,boolean flag){
	if(flag){
	System.out.println("正在使用"+this.getBrand()+"品牌手機給"+phoneName+"打電話。");
	}else{
	System.out.println("正在接"+phoneName+"的電話。");
	}
	}
//2.發信息方法
	public void sendMsg(String pnum,boolean flag){
	if(flag){
	System.out.println("給"+pnum+"手機號碼發的資訊。");
	}else{
	System.out.println("收到來自"+pnum+"的簡訊息。");
	}
	}
	public void show(){
		System.out.println(this.brand);
		System.out.println(this.price);
		System.out.println(this.color);
	}
//定義一個測試類,例項化手機類,分別呼叫以上四個方法。
	public static void main(String[] args) {
	Demo01Phone phone1 = new Demo01Phone();
	phone1.setBrand("蘋果");
	phone1.setPrice("8000.0");
	phone1.setColor("黑");
	phone1.phoneCall("張三", true);
	phone1.phoneCall("張三", false);
	phone1.sendMsg("張三", true);
	phone1.sendMsg("張三", false);
	phone1.show();
	System.out.println("-----------");


	Demo01Phone phone2 = new Demo01Phone();
	phone2.setBrand("華為");
	phone2.setPrice("6000.0");
	phone2.setColor("白");
	phone2.phoneCall("李四", true);
	phone2.phoneCall("李四", false);
	phone2.sendMsg("李四", true);
	phone2.sendMsg("李四", false);
	phone2.show();
	System.out.println("-----------");


	Demo01Phone phone3 = new Demo01Phone();
	phone3.setBrand("三星");
	phone3.setPrice("4000.0");
	phone3.setColor("紅");
	phone3.phoneCall("王五", true);
	phone3.phoneCall("王五", false);
	phone3.sendMsg("王五", true);
	phone3.sendMsg("王五", false);
	phone3.show();
	System.out.println("-----------");


	}
}