类和对象(2)任务1:编写程序实现查看手机配置与功能

  • Post author:
  • Post category:其他


任务1:编写程序实现查看手机配置与功能

(1)定义一个手机类Phone。

(2)手机具有属性:品牌(brand)、型号(type)、价格(price)、操作系统(os)和内存(memory)。因此,需要在手机类中定义品牌(brand)、型号(type)、价格(price)、操作系统(os)和内存(memory)的变量。

(3)手机具有功能:查看手机信息(about())、打电话(call(String no))、玩游戏(playGame())、下载音乐(downloadMusic())、播放音乐(playMusic())。所以,可以定义对应的方法about()、call()、playGame()、downloadMusic()、playMusic()。

(4)创建了两个手机对象,并输出了这两个手机对象的属性及功能。

任务1代码如下:

package test1;

public class Phone {
	private String brand;
	private int type;
	private int price;
	private String os;
	private String memory;
	
	
	public String getBrand() {
		return brand;
	}
	
	public void setBrand(String brand) {
		this.brand=brand;
	}
	
	public int getType() {
		return type;
	}
	
	public void setType(int type) {
		this.type=type;
	}
	
	public int getprice() {
		return price;
	}
	
	public void setPrice(int price) {
		this.price=price;
	}
	
	public String getOs() {
		return os;
	}
	
	public void setOs(String os) {
		this.os=os;
	}
	
	public String getMemory() {
		return memory;
	}
	
	public void setMemory(String memory) {
		this.memory=memory;
	}
	
	public Phone() {
		
	}
	public Phone(String brand,int type,int price,String os,String memory) {
		this.brand=brand;
		this.type=type;
		this.price=price;
		this.os=os;
		this.memory=memory;
	}
	
	public String about() {
		return "手机品牌为"+this.brand+",手机型号为"+this.type;
	}
	public String call(String no) {
		return "使用"+this.brand+"手机打电话给"+no;
	}
	public String playGame() {
		return ""+this.brand+"手机玩游戏";
	}
	public String downloadMusic() {
		return ""+this.brand+"手机下载音乐";
	}
	public String playMusic() {
		return ""+this.brand+"手机博放音乐";
	}
	

}
package test1;

public class Main {
	public static void main(String[] args) {
		Phone phone1=new Phone();
		phone1.setBrand("vivo");
		phone1.setType(1);
		phone1.setPrice(2000);
		phone1.setOs("windows");
		phone1.setMemory("128G");
		
		System.out.println(phone1.about());
		System.out.println(phone1.call("123456"));
		System.out.println(phone1.playGame());
		System.out.println(phone1.downloadMusic());
		System.out.println(phone1.playMusic());
		System.out.println("-------------------------------");
		Phone phone2=new Phone();
		phone2.setBrand("oppo");
		phone2.setType(2);
		phone2.setPrice(2300);
		phone2.setOs("linux");
		phone2.setMemory("248G");
		
		System.out.println(phone2.about());
		System.out.println(phone2.call("24689"));
		System.out.println(phone2.playGame());
		System.out.println(phone2.downloadMusic());
		System.out.println(phone2.playMusic());
	}

}

运行结果如下:



版权声明:本文为you1903010302原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。