代码:
1 /* 2 test.java 3 */ 4 package test; 5 public class test { 6 public static void main(String args[] ) 7 { 8 CPU ccp= new CPU(); 9 HardDisk hhd=new HardDisk(); 10 PC pc =new PC(); 11 ccp.setSpeed(2200); 12 hhd.setAmount(200); 13 pc.setCPU(ccp); 14 pc.setHardDisk(hhd); 15 pc.show(); 16 } 17 18 }
1 /* 2 PC.java 3 */ 4 package test; 5 public class PC { 6 CPU cpu ; 7 HardDisk HD ; 8 void setCPU(CPU cp) { cpu=cp; } 9 void setHardDisk(HardDisk hd){ HD=hd ;} 10 void show() 11 { 12 System.out.println("CPU的Speed: "+cpu.getSpeed()); 13 System.out.println("HardDisk: "+HD.getAmount()); 14 } 15 }
1 /* 2 CPU.java 3 */ 4 package test; 5 6 public class CPU { 7 int speed; 8 void setSpeed(int m){speed=m;} 9 int getSpeed(){return speed ;} 10 }
1 /* 2 HardDisk.java 3 */ 4 package test; 5 6 public class HardDisk { 7 8 int amount; 9 void setAmount(int m){ amount=m;} 10 int getAmount(){return amount;} 11 }