开发者

How to force my method to accept objects from external software?

开发者 https://www.devze.com 2022-12-27 11:25 出处:网络
I have a method which needs to take a callback object as an argument and then (at the moment when it\'s needed) my method will call a specific method of the callback object.

I have a method which needs to take a callback object as an argument and then (at the moment when it's needed) my method will call a specific method of the callback object.

I wrote a class called Manager which has a method called addListener. As the argument for this method I need to use a callback object which is defined by the external software. So, I define the addListener in the following way: public void addListener(Listener listener).

Of course Eclipse complains because it does not know what Listener is (because the Listener is defined by the external software). The only think that I know (should know) about the Listener is that it has a method called doSomething. So, to pleasure Eclipse I add an interface before my Manager class:

interface Listener {
    void doSomething();
}

public class CTManager {
...

The problem seems to be solved but then I try to test my software. So, I create a class called test. In this class I create an instance of the Manager class and try to use addListener method of this instance.

I also create a class Listener, instantiate it and give the instance to the addListener. And it's the place where the problem appears. Eclipse writes that addListener is not applicable to the given argument. I think it's because it expect something from my Listenr interface but gets something from the Listener cl开发者_StackOverflowass.

How can I solve this problem?


You are on the correct path to a solution. In order to let 2 pieces of software to interface with eachother the 2 pieces need to agree on the contract that is made between them.

A callback mechanism like you describe is quite common. The contract here is the Listener interface which your part uses to call the doSomething() method on the callback object, and which the other part needs to implement in order to offer a valid listener object.

The way Java handles these contacts is by having the classloaders declare that the interface of one part of software is compatible to (or equal to) the interface the other part uses.

This means that both the class or interface names must coincide, but also their packages.

When you built your manager, you also provide an interface definition of the contract, say in:

com.your.program.manager.Listener

This means you will have to give a copy of the file

com/your/program/manager/Listener.java

to the party that buids the callback object, so that they can import the same Listener definition as your manager uses. If both parts use the same definition, the contact will work.


The class Listener that you instanciate must implements the Listener interface you declared to be parameter of the addListener function...


It looks like the Listener interface that you've created has a different package name to the 3rd-party Listener object that you're trying to accept as an argument.

My preferred solution would be to include the 3rd party library containing the Listener object on your Eclipse Build Path, and then you can actually reference the 3rd-party object directly.

If there's some reason why you can't do this, make sure that your Listener interface is in the same package as the 3rd-party Listener class, so that the fully-qualified names of your interface and the 3rd-party class are identical.


I think this is probably a build path problem in Eclipse. Check that the JAR file containing the Listener class is on the build path.

Another possibility is that you are missing an import some.package.Listener; statement in the source code of your CTManager class.

0

精彩评论

暂无评论...
验证码 换一张
取 消