简体   繁体   中英

Java Multiple Inheritance without Source Code

I have a class called Thing and a class called Robot . Thing has a public void setBlocksExit() . Robot has some methods I also desire.

I have extended Robot but I also want setBlocksExit() from Thing . I would make an interface that has setBlocksExit() and then make a class like:

public class C extends Robot implements BlockExit {}

The problem is I don't have access to the source code for Thing and Robot . I am using an educational package 'becker.jar' and all of the code is compiled so I can't access it to extract interfaces. What are my options?

One alternative would be to wrap Robot and Thing in your own wrapper classes

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.

Another alternative would be to compose a class with both Robot and Thing as member fields

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.
  • Extend Robot and have a reference to a Thing object which you delegate setBlocksExit calls to.
  • Create a fresh class and have a reference to a Robot and a reference to a Thing and delegate calls to these two objects.

If you're using an IDE such as Eclipse you can even "extract interfaces" and generate delegate methods automatically.

Option 1:

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

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

    ...
}

Option 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:

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:

I'm sure whatever IDE you're using has a similar feature.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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