package cpu;
public class CPU {
int speed;
void setSpeed(int m){ //设置cpu速度
speed=m;
}
int getSpeed(){
return speed; //返回cpu速度
}
}
package cpu;
public class PC {
CPU cpu;
HardDisk HD;
void setCPU(CPU c){
cpu=c; //将参数c的值赋值给cpu
}
void setHardDisk(HardDisk h){
HD=h; //将参数H的值赋值给HD
}
void show(){ //调用方法,显示cpu的速度和硬盘容量
System.out.println("CPU的速度为:"+cpu.getSpeed());
System.out.println("硬盘的容量为:"+HD.getAmount());
}
}
package cpu;
public class HardDisk {
int amount;
void setAmount(int m){ //将参数m的值赋值给amount
amount=m;
}
int getAmount(){
return amount; //返回amount的值
}
}
package cpu;
public class Test {
public static void main(String[] args) {
// TODO 自动生成的方法存根
CPU cpu=new CPU(); //创建CPU对象
cpu.setSpeed(2200); //将值设置为2200
HardDisk disk=new HardDisk(); //创建HardDisk对象
disk.setAmount(200); //将值设置为200
PC pc=new PC(); //创建PC对象
pc.setCPU(cpu); //调用方法
pc.setHardDisk(disk);
pc.show();
}
}