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

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

代码:

java第四章编程题(初学篇)
 1 /*
 2    test.java
 3 */
 4 package test;
 5 public class test {
 6     public static void main(String args[] )
 7     {
 8         CPU ccp= new CPU();
 9         HardDisk hhd=new HardDisk();
10         PC pc =new PC();
11         ccp.setSpeed(2200);
12         hhd.setAmount(200);
13         pc.setCPU(ccp);
14         pc.setHardDisk(hhd);
15         pc.show();
16     }
17 
18 }
View Code
java第四章编程题(初学篇)
 1 /*
 2   PC.java
 3 */
 4 package test;
 5 public class PC {
 6     CPU cpu ;
 7     HardDisk HD ;
 8     void setCPU(CPU cp)  { cpu=cp;  }
 9     void setHardDisk(HardDisk hd){ HD=hd ;}
10     void show()
11     {
12         System.out.println("CPU的Speed: "+cpu.getSpeed());
13         System.out.println("HardDisk: "+HD.getAmount());
14     }
15 }
View Code
java第四章编程题(初学篇)
 1 /* 
 2   CPU.java
 3 */
 4 package test;
 5 
 6 public class CPU {
 7      int speed;
 8      void setSpeed(int m){speed=m;}
 9      int getSpeed(){return speed ;}
10 }
View Code
java第四章编程题(初学篇)
 1 /*
 2   HardDisk.java
 3 */
 4 package test;
 5 
 6 public class HardDisk {
 7 
 8     int amount;
 9     void setAmount(int m){    amount=m;}
10     int getAmount(){return amount;}
11 }
View Code

 

java第四章编程题(初学篇),布布扣,bubuko.com

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

上一篇:JAVA中int、String的类型转换


下一篇:使用GCP的Kubernetes部署项目时出现的编译问题