Java接口双继承

时间:2015-06-26 00:04:14

标签: java inheritance interface

所以,让我们说我有一个java界面能量:

public Interface IEnergyNetwork {

    Voltage getVoltage();

    Resistance getResistance();

    Current getCurrent();
}

每种方法返回的内容非常明显。

现在假设我有两个实现同一个接口的类,一个类是重要的类,另一个类只是一个虚拟安全事件(如果我的程序出现任何问题,比如bug或其他什么)。 / p>

虚拟课程:

public class DummyEnergyNetwork implements IEnergyNetwork {

    @Override
    public Voltage getVoltage(){
    return null;
    }

    @Override
    public Resistance getResistance(){
    return null;
    }

    @Override
    public Current getCurrent(){
    return null;
    }

}

我将与之合作的课程:

public class EnergyNetwork implements IEnergyNetwork{
    @Override
    public Voltage getVoltage(){
    //work with the code that I need
    }

    @Override
    public Resistance getResistance(){
    //work with the code that I need
    }

    @Override
    public Current getCurrent(){
    //work with the code that I need
    }
}

现在,如果我想从另一个类调用getCurrent()方法,例如:

public class PowerGenerator {

    public void addPower(double power){
        IEnergyNetwork network = new DummyEnergyNetwork();
        double current = network.getCurrent().size(); //assuming that the type current has a method that returns its value in double
        //do the rest of the code
    }
}

我怀疑我将返回哪个:DummyEnergyNetwork类中的null值或EnergyNetwork类中的当前值?

顺便说一下,这是我在Minecraft mod Botania的Github上看到的一些代码的改编。如果您无法理解我的示例,我的问题的根源在SubTileFunctional(https://github.com/Vazkii/Botania/blob/master/src/main/java/vazkii/botania/api/subtile/SubTileFunctional.java)类下。

1 个答案:

答案 0 :(得分:2)

  

我的疑问是我将返回哪个:来自的null   DummyEnergyNetwork类或EnergyNetwork类的当前类?

IEnergyNetwork network = new DummyEnergyNetwork();

作为引用network指向DummyEnergyNetwork,因此将调用DummyEnergyNetwork方法。

相关问题