简体   繁体   English

没有源代码的Java多重继承

[英]Java Multiple Inheritance without Source Code

I have a class called Thing and a class called Robot . 我有一个名为Thing的课程和一个名为Robot的课程。 Thing has a public void setBlocksExit() . Thing具有公共无效setBlocksExit() Robot has some methods I also desire. Robot有一些我也想要的方法。

I have extended Robot but I also want setBlocksExit() from Thing . 我已经扩展了Robot但是我还想要Thing setBlocksExit() I would make an interface that has setBlocksExit() and then make a class like: 我将创建一个具有setBlocksExit()的接口,然后创建一个类似以下的类:

public class C extends Robot implements BlockExit {}

The problem is I don't have access to the source code for Thing and Robot . 问题是我无法访问ThingRobot的源代码。 I am using an educational package 'becker.jar' and all of the code is compiled so I can't access it to extract interfaces. 我正在使用教育软件包'becker.jar' ,所有代码都已编译,因此我无法访问它来提取接口。 What are my options? 我有什么选择?

One alternative would be to wrap Robot and Thing in your own wrapper classes 一种选择是将RobotThing包装在您自己的包装器类中

public class MyRobot extends Robot implements IBlockingObject

and

public class MyThing extends Thing implements IBlockingObject

where you can force the interface 您可以在其中强制界面

interface IBlockingObject{
   void setBlocksExit(boolean blocksExit);
}

Then you can use IBlockingObject reliably elsewhere in your code, without much overhead. 然后,您可以在代码中的其他IBlockingObject可靠地使用IBlockingObject ,而无需太多开销。

Another alternative would be to compose a class with both Robot and Thing as member fields 另一种选择是组成一个以RobotThing作为成员字段的类

Something like 就像是

public class RobotThing extends IBlockingObject{
  // This is now a robot thing...
  private Robot mRobot;
  private Thing mThing;

  @Override 
  public void setBlocksExit(boolean blocksExit){ 
    mRobot.setBlocksExit(blocksExit);
    mThing.setBlocksExit(blocksExit);
  }
}

I assume the first would be more flexible for you in the long run. 我认为从长远来看,第一个对您来说将更加灵活。

Your options are the following: 您的选择如下:

  • Extend Thing and have a reference to a Robot which you delegate all Robot methods to. 扩展Thing并引用一个将所有Robot方法委托给它的Robot的引用。
  • Extend Robot and have a reference to a Thing object which you delegate setBlocksExit calls to. 扩展Robot并引用您将setBlocksExit调用委托给Thing对象的对象。
  • Create a fresh class and have a reference to a Robot and a reference to a Thing and delegate calls to these two objects. 创建一个新的类,并引用一个Robot和一个Thing然后将调用委托给这两个对象。

If you're using an IDE such as Eclipse you can even "extract interfaces" and generate delegate methods automatically. 如果您使用的是Eclipse之类的IDE,您甚至可以“提取接口”并自动生成委托方法。

Option 1: 选项1:

class C extends Thing {
    final Robot robot;
    public C(Robot robot) {
        this.robot = robot;
    }

    public int robotMethod1() {
        return robot.robotMethod1();
    }

    ...
}

Option 2: 选项2:

class C extends Robot {
    final Thing thing;
    public C(Thing thing) {
        this.thing = thing;
    }

    public void setBlocksExit(boolean flag) {
        return thing.setBlocksExit(flag);
    }

    ...
}

Option 3: 选项3:

class C {
    final Thing thing;
    final Robot robot;
    public C(Thing thing, Robot robot) {
        this.thing = thing;
        this.robot = robot;
    }

    public void setBlocksExit(boolean flag) {
        return thing.setBlocksExit(flag);
    }

    public int robotMethod1() {
        return robot.robotMethod1();
    }
    ...
}

If you're using Eclipse you could use this feature: 如果您使用的是Eclipse,则可以使用以下功能:

I'm sure whatever IDE you're using has a similar feature. 我确定您使用的任何IDE都具有类似的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM