2

プロジェクト内のいくつかのクラスをリファクタリングする必要があり、オーバーライドの問題を修正する方法について質問があります。

私は、呼び出さAccSysChannelれた別のクラスから継承し、 (以下のコード) で示されるSwitchChannelメソッドを呼び出したクラスを持っています。blockUntilGetMessageLength()@Override

SwitchChannel以前は というメソッドがblockUntilGetMessageLength()あり、 という新しいクラスに移動する必要がありましたActiveSocket

私の問題は、現在のメソッドを使用する方法が@Override blockUntilGetMessageLength()わからないことです。`AccSysChannel クラスには他にも多数のメソッドが含まれているため、 は l からの継承を維持する必要があります。AccSysChannelActiveSocketAccSysChannelSwitchChanne

私の質問は、どうすれば@Override問題を解決できますか? これが理にかなっていることを願っています。ヘルプ/指示をいただければ幸いです。ありがとうございました。

これが私のものblockUntilGetMessageLength()ですAccSysChannel

....

@Override
protected int blockUntilGetMessageLength() {
    byte[] b = new byte[2];
    byte[] other = new byte[14];
    int size = -1;
    String otherStr;

    try {
        Iterator<ActiveSocket> it = activeSockets.iterator();
        while (it.hasNext()) {
            ActiveSocket activeSocket = it.next();
            DataInputStream dataInputStream = activeSocket.getDataInputStream(); 
            if (dataInputStream.markSupported()) {
                while (size == -1) {
                    dataInputStream.mark(14);
                    dataInputStream.readFully(other, 0, 14);
                    dataInputStream.reset();
                    otherStr = new String(other);
                    LOGGER.trace("Checking slice of the message: " + otherStr);
                    size = otherStr.indexOf("<AT");
                    if (size == -1){
                        size = otherStr.indexOf("<DSCREDIT");
                    }
                    LOGGER.trace("Found tag at index: " + size);
                    switch (size) {
                        case 0:
                        case 1:
                            // intentional fallthrough
                        case -1:
                            if (size == 0 || size == 1) {
                                size = -1;
                            }
                            dataInputStream.readFully(other, 0, 10);
                            break;
                        default:
                            // consume the bytes preceding the size bytes
                            dataInputStream.readFully(other, 0, size - 2);
                            // read the size bytes
                            dataInputStream.readFully(b, 0, 2);
                            size = (((((int) b[0]) & BYTEMASK) << BYTESHIFT) | (((int) b[1]) & BYTEMASK));
                            LOGGER.trace("Setting size to: " + size);
                    }
                }
            }
        }
    } catch (IOException ex) {
        if (this.shouldTerminate) {
            return 0;
        } else {
            Iterator<ActiveSocket> it = activeSockets.iterator();
            while (it.hasNext()) {
                ActiveSocket activeSocket = it.next();
                Socket socket = activeSocket.getSocket();
                LOGGER.warn(FormatData.formatStack(ex));
                synchronized (socket) {
                    socket.notify();
                }
            }
            return 0;
        }
    }
    return size;
}
4

3 に答える 3

0

簡単です...できません。Java は多重継承をサポートしていません。別のデザイン パターン、おそらく Decorator などを使用するか、またはSwitchChannel継承元にするActiveSocketことができますが、継承を分割することはできません。

于 2013-09-13T12:31:37.633 に答える