用类描述计算机中CPU的速度和硬盘的容量。(Java)
PC.java
public class PC { CPU cpu; HardDisk HD; public void setCPU(CPU c) { this.cpu = c; } public void setHardDisk(HardDisk h) { HD = h; } public void show() { System.out.println("cpu: " +cpu.getSeepd()); System.out.println("Hd: "+HD.getAmount()); } }
CPU.java
public class CPU { private int speed; public void setSeepd(int m) { this.speed = m; } public int getSeepd() { return speed; } }
HardDisk.java
public class HardDisk { int amount; public void setAmount(int m) { this.amount = m; } public int getAmount() { return amount; } }
Test.java
public class Test { public static void main(String[] args) { CPU cpu =new CPU(); cpu.setSeepd(2000); HardDisk disk = new HardDisk(); disk.setAmount(200); PC pc =new PC(); pc.setCPU(cpu); pc.setHardDisk(disk); pc.show(); } }
运行结果: