您的位置:首页 > 理论基础

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

WeirdoL 2017-10-11 11:51 19 查看
public class CPU {
int speed;
public int getSpeed() {
return speed;
}
public void setSpeed(int m) {
speed=m;
}
}

public class HardDisk {
int amount;
public int getAmount() {
return amount;
}
public void setAmount(int m) {
amount=m;
}
}

public class PC {
CPU cpu=new CPU();
public void setCPU(CPU c) {
cpu=c;
}
HardDisk HD=new HardDisk();
public void setHardDisk(HardDisk h) {
HD=h;
}
public void show() {
System.out.println("cpu的速度是:"+cpu.getSpeed()+"硬盘的容量是:"+HD.getAmount());
}
}

public class Test {

public static void main(String[] args) {
// TODO Auto-generated method stub
CPU cpu=new CPU();
cpu.setSpeed(2200);
HardDisk disk=new HardDisk();
disk.setAmount(200);
PC pc=new PC();
pc.setCPU(cpu);
pc.setHardDisk(disk);
pc.show();
}
}


标签: