简体   繁体   中英

Java using interface as a callback

I am trying to implement a callback procedure by having a class implement and interface, and then pass that class as an object (of the interface) to another class. However, I am receiving the error: "The constructor ClassB(TestMe) is undefined". I thought that I was doing this correctly, I don't know what I am doing wrong. Can someone please offer some advice? My code is below:

I have an interface:

public interface RequestResults {

     public void requestFailed(String message);

     public void requestSucceeded(String xml);

}

And I have a class that implements the interface:

public class TestMe implements RequestResults {

    public TestMe() {

        ClassB b = new ClassB(this);

    }

    public void requestFailed(String message) {
        // TODO Auto-generated method stub

    }

    public void requestSucceeded(String xml) {
        // TODO Auto-generated method stub

    }
}

Finally, I have a class that is instantiated in the prior class:

  public class ClassB {

    RequestResults results;

    public ClassB(RequestResults results) {

        this.results = results;

    }

}

Thanks!

I've coded the whole thing on my side and everything compiled. Cleaning and compiling it afresh might help.

I think your code is correct. Try to compile everything again. Maybe you have been using an old version of TestMe .

Hmm. compiles just fine for me. Here is an ideone SSCCE .

也为我编译....你必须在eclipse中清理你的项目有时会发生

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