题目要求
分别创建四个类
CPU
public class CPU
{
int speed;
int getSpeed()
{
return speed;
}
void setSpeed(int speed)
{
this.speed=speed;
}
}
HardDisk
public class HardDisk
{
int amount;
int getAmount()
{
return amount;
}
void setAmount(int amount)
{
this.amount=amount;
}
}
PCpublic class PC
{
CPU cpu;
HardDisk HDD;
void setCPU(CPU cpu)
{
this.cpu=cpu;
}
void setHardDisk(HardDisk HDD)
{
this.HDD=HDD;
}
void show()
{
System.out.println("CPU RUNNING SPEED:"+cpu.getSpeed());
System.out.println("HardDisk RUNNING SPEED:"+HDD.getAmount());
}
}
最后主类test
public class Test
{
public static void main(String args[])
{
CPU cpu=new CPU();
HardDisk HDD=new HardDisk();
PC pc=new PC();
cpu.setSpeed(2200);
HDD.setAmount(200);
pc.setCPU(cpu);
pc.setHardDisk(HDD);
pc.show();
}
}
效果图--------------------------