public class CPU { int speed; //获得speed的值 public void setSpeed(int m){ speed = m; } //返回speed的值 public int getSpeed(){ return speed; } }
public class HardDisk { int amount; //获得amount的值 public void setSpeed(int m){ amount = m; } //返回amount的值 public int getSpeed(){ return amount; } }
public class PC { CPU cpu = new CPU(); HardDisk HD = new HardDisk(); //将参数c的值赋值给cpu public void setCPU(CPU c){ cpu = c; } //将参数h的值赋值给HD public void setHardDisk(HardDisk h){ HD = h; } //显示cpu的速度和硬盘的容量 public void show(){ System.out.println(cpu.speed); System.out.println(HD.amount); } }
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.setSpeed(200); pc.setCPU(cpu); pc.setHardDisk(disk); pc.show(); } }