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

                                                                用4个类 PC,CPU,HardDisk,Test,其中Test是主类。。。。。。。。

public class CPU {

int speed;
void setSpeed(int m){
speed=m;
}
int getSpeed(){
return speed;
}
}


public class HardDisk {
int count;

void setAmount(int m){
count=m;
}
int getAmount(){
return count;
}
}


public class PC {
CPU cpu;
HardDisk HD;
void setCPU(CPU cpu){
this.cpu=cpu;
}
void setHardDisk(HardDisk HD){
this.HD=HD;
}
void show(){
System.out.print("CPU的速度:"+cpu.getSpeed()+"\n"+"硬盘的容量:"+HD.getAmount());
}
}


public class Test {
public static void main(String args[]){
CPU cpu=new CPU();
HardDisk disk=new HardDisk();
PC pc=new PC();
cpu.setSpeed(2200);
disk.setAmount(200);
pc.setCPU(cpu);
pc.setHardDisk(disk);
pc.show();
}
}


运算结果:CPU的速度:2200
                    硬盘的容量:200

你可能感兴趣的:(java)