java 对象的组合,一个类组合到另一个类中(例如手机卡装到手机上)

Example4_9.java

public class Example4_9 {
   public static void main(String args[]) {
       SIM simOne = new SIM(13889776509L);
       MobileTelephone mobile = new MobileTelephone();
       mobile.setSIM(simOne);
       System.out.println("手机号码:"+mobile.lookNumber()); 
       SIM simTwo = new SIM(15967563567L);
       mobile.setSIM(simTwo);
       System.out.println("手机号码:"+mobile.lookNumber());  
    }
}

MobileTelephone.java

public class MobileTelephone { 
    SIM sim;
    void setSIM(SIM card) {
        sim = card;
    } 
    long lookNumber(){
        return sim.getNumber();
    }
}

SIM.java

public class SIM {
    long number;  
    SIM(long number){
        this.number = number;
    }
    long getNumber() {
        return number;
    }
}

 

posted @ 2016-10-23 16:52  我有一壶酒  阅读(490)  评论(0编辑  收藏  举报