为什么一个子类的接口方法为实现,却实现了另一个子类的接口方法?

1.我首先在CallService接口中定义了call()方法

package ProjectOne;

public interface CallService {

    /**
     * 通话
     * @param time
     * @param card
     */
    public void call(int time,MobileCard card);
}

2.然后定义了类SupperPackage子类继承ServicePackage父类,并且实现CallService,NetService,SendService三个接口

package ProjectOne;

public class SuperPackage extends ServicePackage implements CallService,NetService,SendService{
    private int smstalkTime;
    private int smsCount;
    private float smsflow;

    /**
     * 构造方法
     */
    public SuperPackage() {
        super(78);
        this.smstalkTime = 200;
        this.smsCount = 50;
        this.smsflow = 5*1024;
    }

    @Override
    public void call(int time, MobileCard card) {
        int temp=1;
        while (temp++ <= time){
            if(smstalkTime- card.getRealTalkTime() >= 1){
                card.setRealTalkTime(card.getRealTalkTime() + 1);
            }else if(card.getMoney() >= 0.2){
                card.setRealTalkTime(card.getRealTalkTime() + 1);
                card.setConsumAmount(card.getConsumAmount() + 0.2);
                card.setMoney(card.getMoney() - 0.2);
            }
        }
        System.out.println("super");
    }

    @Override
    public void net(float flow, MobileCard card) {
        int temp=1;
        while (temp++ <= flow){
            if(smsflow - card.getRealFlow() >= 1){
                card.setRealFlow(card.getRealFlow() + 1);
            }else if(card.getMoney() >= 1){
                card.setRealFlow(card.getRealFlow() + 1);
                card.setConsumAmount(card.getConsumAmount() + 1);
                card.setMoney(card.getMoney() - 1);
            }
        }

        System.out.println("super");

    }

    @Override
    public void send(int count, MobileCard card) {

        int temp=1;
        while (temp++ <= count){

            if(smsCount - card.getRealSMScount() >= 1){
                card.setRealSMScount(card.getRealSMScount() + 1);
            }else if(card.getMoney() >= 0.1){
                card.setRealSMScount(card.getRealSMScount()+1);
                card.setConsumAmount(card.getConsumAmount()+0.1);
                card.setMoney(card.getMoney() - 0.1);
            }
        }

        System.out.println("super");
    }

    public String toString(){
        return "SuperPackage套餐"+"\n通话时长"+smstalkTime+"\n短信条数"+smsCount+"\n上网流量"+smsflow+"\n资费"+getPrice();
    }
}

3.我的TalkPackage子类同样继承了ServicePackage父类,并实现接口

package ProjectOne;                                                                        
                                                                                           
public class TalkPackage extends ServicePackage implements CallService,SendService{        
    private int smtalkTime;                                                                
    private int smCount;                                                                   
                                                                                           
    public TalkPackage() {                                                                 
        super(58);                                                                         
        this.smtalkTime = 500;                                                             
        this.smCount = 30;                                                                 
    }                                                                                      
                                                                                           
    @Override                                                                              
    public void send(int count, MobileCard card) {                                         
        int temp = 1;                                                                      
        while (temp++ <= count){                                                           
            if(smCount - card.getRealSMScount() >= 1){                                     
                card.setRealSMScount(card.getRealSMScount() + 1);                          
            }else if(card.getMoney() >= 0.1){                                              
                card.setRealSMScount(card.getRealSMScount() + 1);                          
                card.setConsumAmount(card.getConsumAmount() + 0.2);                        
                card.setMoney(card.getMoney() - 0.2);                                      
            }                                                                              
        }                                                                                  
        System.out.println("talk");                                                        
    }                                                                                      
                                                                                           
    @Override                                                                              
    public void call(int time, MobileCard card) {                                          
        int temp=1;                                                                        
        while (temp++ <= time){                                                            
            if(smtalkTime- card.getRealTalkTime() >= 1){                                   
                card.setRealTalkTime(card.getRealTalkTime() + 1);                          
            }else if(card.getMoney() >= 0.2){                                              
                card.setRealTalkTime(card.getRealTalkTime() + 1);                          
                card.setConsumAmount(card.getConsumAmount() + 0.2);                        
                card.setMoney(card.getMoney() - 0.2);                                      
            }                                                                              
        }                                                                                  
        System.out.println("talk");                                                        
    }                                                                                      

4.但是当我测试运行,定义TalkPackage类的new对象,调用接口call()方法时,实现的却是SupperPackage类的call()方法,为什么?

package ProjectOne;

public class test {
    public static void main(String[] args) {
        /*//选一个套餐
        SuperPackage superPackage = new SuperPackage();
        MobileCard card = new MobileCard("18800988726",superPackage,100);

        //实现方法
        card.send(100);
        card.show();
        card.call(60);
        card.show();
        card.net(5121);
        card.show();
*/
        //套餐二
        TalkPackage talkPackage = new TalkPackage();
        MobileCard card1 = new MobileCard("15567642536",talkPackage,100);
        //实现方法
        card1.call(200);
        card1.show();
}
}

为什么?????????为什么结果是这样的????来个大神帮我解决一下好吗?

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值