CPU类:
public class CPU {
int speed;
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public CPU(int speed) {
this.speed = speed;
}
}
HardDisk类:
public class HardDisk {
int amount;
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public HardDisk(int amount) {
this.amount = amount;
}
}
PC类:
public class PC {
CPU cpu;
HardDisk harddisk;
public void show(){
System.out.println("PC的cpu速度为:"+cpu.speed+"M");
System.out.println("PC的硬盘容量为:"+harddisk.amount+"Gb");
}
public void setCpu(CPU cpu) {
this.cpu = cpu;
}
public void setHarddisk(HardDisk harddisk) {
this.harddisk = harddisk;
}
public PC(CPU cpu, HardDisk harddisk) {
this.cpu = cpu;
this.harddisk = harddisk;
}
public PC() {
}
}
Test类:
public class Test {
public static void main(String[] args) {
CPU cpu=new CPU(3000);
HardDisk harddisk=new HardDisk(1024);
PC pc=new PC();
pc.setCpu(cpu);
pc.setHarddisk(harddisk);
pc.show();
}
}
运行结果:
PC的cpu速度为:3000M
PC的硬盘容量为:1024Gb