用类描述计算机中CPU的速度和硬盘的容量

要求Java应用程序中有4个类,名字分别为:PC、CPU、HardDisk和Test,其中Test是主类。

CPU类,代码如下:

package CpuAndHardDisk;

public class CPU {
	private int speed;

	public int getSpeed() {
		return speed;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}
	
}
HardDisk类,代码如下:

package CpuAndHardDisk;

public class HardDisk {
	private int amount;

	public int getAmount() {
		return amount;
	}

	public void setAmount(int amount) {
		this.amount = amount;
	}
	
}
PC类,代码如下:

package CpuAndHardDisk;

public class PC {
	private CPU cpu;
	private HardDisk HD;
	
	public void setCpu(CPU cpu) {
		this.cpu = cpu;
	}

	public void setHD(HardDisk hD) {
		HD = hD;
	}
	
	public void show() {
		System.out.println("CPU速度为:"+cpu.getSpeed()+",磁盘容量为:"+HD.getAmount());
	}
}
Test类,代码如下:

package CpuAndHardDisk;

public class Test {

	public static void main(String[] args) {
		CPU cpu =new CPU();
		cpu.setSpeed(2200);
		HardDisk HD =new HardDisk();
		HD.setAmount(200);
		
		PC pc =new PC();
		pc.setCpu(cpu);
		pc.setHD(HD);
		pc.show();
	}

}
运行效果图:

用类描述计算机中CPU的速度和硬盘的容量_第1张图片



你可能感兴趣的:(java)