java第四章编程题(初学篇)

时间:2021-08-17 05:58:38

java第四章编程题(初学篇)

代码:

 /*
test.java
*/
package test;
public class test {
public static void main(String args[] )
{
CPU ccp= new CPU();
HardDisk hhd=new HardDisk();
PC pc =new PC();
ccp.setSpeed(2200);
hhd.setAmount(200);
pc.setCPU(ccp);
pc.setHardDisk(hhd);
pc.show();
} }
 /*
PC.java
*/
package test;
public class PC {
CPU cpu ;
HardDisk HD ;
void setCPU(CPU cp) { cpu=cp; }
void setHardDisk(HardDisk hd){ HD=hd ;}
void show()
{
System.out.println("CPU的Speed: "+cpu.getSpeed());
System.out.println("HardDisk: "+HD.getAmount());
}
}
 /*
CPU.java
*/
package test; public class CPU {
int speed;
void setSpeed(int m){speed=m;}
int getSpeed(){return speed ;}
}
 /*
HardDisk.java
*/
package test; public class HardDisk { int amount;
void setAmount(int m){ amount=m;}
int getAmount(){return amount;}
}