如何在类之间执行JAVA回调?

我来自JavaScript,其中的回调非常简单。我正在尝试将它们实施到JAVA中,但没有成功。


我有一个家长班:


import java.net.Socket;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;


public class Server {

    ExecutorService workers = Executors.newFixedThreadPool(10);

    private ServerConnections serverConnectionHandler;


    public Server(int _address) {

        System.out.println("Starting Server...");

        serverConnectionHandler = new ServerConnections(_address);


        serverConnectionHandler.newConnection = function(Socket _socket) {

            System.out.println("A function of my child class was called.");

        };


        workers.execute(serverConnectionHandler);


        System.out.println("Do something else...");

    }

}

然后我有一个子类,从父类中调用:


import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.logging.Level;

import java.util.logging.Logger;


public class ServerConnections implements Runnable {

    private int serverPort;

    private ServerSocket mainSocket;


    public ServerConnections(int _serverPort) {

        serverPort = _serverPort;

    }


    @Override

    public void run() {

        System.out.println("Starting Server Thread...");


        try {

            mainSocket = new ServerSocket(serverPort);


            while (true) {

                newConnection(mainSocket.accept());

            }

        } catch (IOException ex) {

            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);

        }

    }


    public void newConnection(Socket _socket) {


    }

}

什么是实施


serverConnectionHandler.newConnection = function(Socket _socket) {

    System.out.println("A function of my child class was called.");

};

部分,在Parent类中,哪个显然不正确?


紫衣仙女
浏览 327回答 3
3回答

大话西游666

使用观察者模式。它是这样的:interface MyListener{&nbsp; &nbsp; void somethingHappened();}public class MyForm implements MyListener{&nbsp; &nbsp; MyClass myClass;&nbsp; &nbsp; public MyForm(){&nbsp; &nbsp; &nbsp; &nbsp; this.myClass = new MyClass();&nbsp; &nbsp; &nbsp; &nbsp; myClass.addListener(this);&nbsp; &nbsp; }&nbsp; &nbsp; public void somethingHappened(){&nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Called me!");&nbsp; &nbsp; }}public class MyClass{&nbsp; &nbsp; private List<MyListener> listeners = new ArrayList<MyListener>();&nbsp; &nbsp; public void addListener(MyListener listener) {&nbsp; &nbsp; &nbsp; &nbsp; listeners.add(listener);&nbsp; &nbsp; }&nbsp; &nbsp; void notifySomethingHappened(){&nbsp; &nbsp; &nbsp; &nbsp; for(MyListener listener : listeners){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listener.somethingHappened();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}您创建一个接口,该接口具有在发生某个事件时要调用的一个或多个方法。然后,事件发生时需要通知的任何类都将实现此接口。由于生产者仅知道侦听器接口,而不是侦听器接口的特定实现,因此这提供了更大的灵活性。在我的示例中:MyClass 是生产者,因为它通知了听众列表。MyListener 是接口。MyForm我对什么时候感兴趣somethingHappened,所以它正在实施MyListener和注册MyClass。现在,无需直接引用MyClass即可通知MyForm事件MyForm。这是观察者模式的优势,它减少了依赖性并提高了可重用性。

繁花如伊

我不知道这是否是您要寻找的,但是您可以通过将回调传递给子类来实现。首先定义一个通用回调:public interface ITypedCallback<T> {&nbsp; &nbsp; void execute(T type);}在ServerConnections实例上创建一个新的ITypedCallback实例:public Server(int _address) {&nbsp; &nbsp; serverConnectionHandler = new ServerConnections(new ITypedCallback<Socket>() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void execute(Socket socket) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // do something with your socket here&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}在回调对象上调用execute方法。public class ServerConnections implements Runnable {&nbsp; &nbsp; private ITypedCallback<Socket> callback;&nbsp; &nbsp; public ServerConnections(ITypedCallback<Socket> _callback) {&nbsp; &nbsp; &nbsp; &nbsp; callback = _callback;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void run() {&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mainSocket = new ServerSocket(serverPort);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (true) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; callback.execute(mainSocket.accept());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}顺便说一句:我没有检查它是否100%正确,请直接在此处进行编码。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java