javac声称,当我清楚的时候,我没有覆盖抽象类实现中的方法

Gra*_*ost 6 java methods implementation abstract

我会尽可能地简短而重要,但这是一个复杂的问题.我正在Linux平台上用Java编写,无论价值多少.

目标的简短版本:我想要一个名为的抽象类Client,它充当客户端连接的通用容器.Client应该对每个连接进行线程化.我也有一些半测试的代码,它以类似的编码方式与服务器相对应.摘要Client应该被实现为更有形和可实现的东西.在我的例子中,我有一个被调用的类FileClientGui,它使用从服务器接收文件内容并显示它们的基本方法来扩展Client和覆盖所有Client的抽象方法.摘要Client本身就是一个扩展,这使事情变得更加复杂java.lang.Thread.

所以这是我的通用术语文件结构:

/class/path/lib/client/Client.java

/class/path/com/fileclient/FileClientGui.java

这两个文件都引用了其他几个自定义类,但我没有收到任何错误.如果我需要发布这些项目的代码,请告诉我,我会发布它们.

所以我在终端上运行这个很长的javac命令,设置classpath和build目录以及所有需要编译的相关文件.我收到的任何代码的唯一错误是:

com/fileclient/FileClientGui.java:26: com.fileclient.FileClientGui is not abstract and does not override abstract method cleanClients() in lib.client.Client
Run Code Online (Sandbox Code Playgroud)

我的代码(见下文)清楚地实现了Client.java中定义的方法和所有其他抽象方法.我搜索了互联网,看起来大多数遇到这种错误的人都试图做一些像实现一样的东西ActionListener,并对这个实现感到困惑,而且很多时候,这只是一个简单的拼写或大写问题.我已经遍及我的代码,以确保这不是一个简单的"oops"问题.我怀疑它实际上是我的类的名称和某些其他类的名称之间的某种冲突,这些类在某种程度上最终出现在我的类路径或Java的本机框架/库中,但我找不到任何明显的东西.

无论如何,这是我的代码.

Client.java:

package lib.client;

import lib.clientservercore.Connection;
import lib.simplefileaccess.Logger;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.lang.Thread;

/**
 *
 * @author Ryan Jung
 */
public abstract class Client extends Thread {

    ArrayList<Connection> connections;
    boolean isRunning;
    Logger log;

    public Client (String logFile) {
        log = new Logger(logFile);

        log.write("Initializing client...");

        connections = new ArrayList<Connection>(50);

        log.write("Client initialized.");
    }

    public void logOut(String contents) {
        log.write(contents);
    }

    public Logger getLogger() {
        return this.log;
    }

    public ArrayList<Connection> getConnections() {
        return connections;
    }

    public void addConnection(Connection c) {
        connections.add(c);
    }

    public void removeConnection(Connection c) {
        connections.remove(c);
    }

    public boolean getIsRunning() {
        return isRunning;
    }

    public void setIsRunning(boolean r) {
        isRunning = r;
    }

    public Connection connect(String host, int port) {
        log.write("Creating new connection...");

        Socket s;
        Connection c = null;

        // Validate port
        if (port <= 1024 || port > 65536) {
            log.write("Invalid server port: " + port + ".  Using 12321.");
            port = 12321;
        }

        try {
            s = new Socket(host, port);
            c = connectClient(s);
        } catch (IOException exIo) {
            log.write("Could not connect to the server at " + host + ":" + port + ".  Exception: " + exIo.getMessage());
            exIo.printStackTrace();
        }

        log.write("Connected client to " + host + ":" + port);

        return c;
    }

    @Override
    public void run() {
        log.write("Running client.");
        runClient();
        log.write("Client finished running.");
    }

    abstract Connection connectClient(Socket sock);

    abstract void runClient();

    abstract void cleanClients();

}
Run Code Online (Sandbox Code Playgroud)

FileClientGui.java:

package com.fileclient;

import lib.client.Client;
import lib.clientservercore.Connection;
import lib.clientservercore.Connection.ConnectionStatus;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Iterator;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import java.lang.Thread;

/**
 *
 * @author Ryan Jung
 */
public class FileClientGui extends Client {

    JFrame frmMain;
    JPanel pnlMain;
    JPanel pnlConnect;
    JTabbedPane tabConnections;
    JLabel lblHost;
    JLabel lblPort;
    JTextField txtHost;
    JTextField txtPort;
    JButton btnConnect;

    public FileClientGui(String logFile) {
        super(logFile);

        logOut("Initializing client controller...");

        frmMain = new JFrame("Client");
        pnlMain = new JPanel(new BorderLayout());
        pnlConnect = new JPanel(new FlowLayout());
        tabConnections = new JTabbedPane();
        lblHost = new JLabel("Host:");
        lblPort = new JLabel("Port:");
        txtHost = new JTextField("localhost", 10);
        txtPort = new JTextField("12321", 5);
        btnConnect = new JButton("Connect");

        frmMain.setSize(450, 600);
        frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmMain.add(pnlMain);
        pnlMain.add(pnlConnect, BorderLayout.NORTH);
        pnlMain.add(tabConnections, BorderLayout.CENTER);
        pnlConnect.add(lblHost);
        pnlConnect.add(txtHost);
        pnlConnect.add(lblPort);
        pnlConnect.add(txtPort);
        pnlConnect.add(btnConnect);

        btnConnect.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String host = txtHost.getText();
                    int port = Integer.parseInt(txtPort.getText());
                    try {
                        Socket sock = new Socket(host, port);
                        FileClientConnectionGui c = (FileClientConnectionGui)(connectClient(sock));
                        tabConnections.addTab(c.getInetAddress().toString(), c.getMainPanel());
                    } catch (UnknownHostException ex) {
                        logOut("Can't find host: " + host + ".  Exception: " + ex.getMessage());
                        ex.printStackTrace();
                    } catch (IOException ex) {
                        logOut("Exception: " + ex.getMessage());
                        ex.printStackTrace();
                    }
                }
            }
        );

        frmMain.setVisible(true);

        logOut("Client controller initialized.");

    }

    public void removeConnection(FileClientConnectionGui c) {
        logOut("Removing connection: " + c.getInetAddress().toString());
        tabConnections.remove(c.getMainPanel());
        logOut("Removed connection.");
    }

    Connection connectClient(Socket sock) {
        logOut("Client controller is creating a new connection...");
        FileClientConnectionGui c = new FileClientConnectionGui(sock, getLogger(), this);
        addConnection(c);
        c.start();
        logOut("Client controller created a new connection.");
        return c;
    }

    void runClient() {
        setIsRunning(true);
        logOut("Client controller is running.");

        while (getIsRunning()) {
            cleanClients();
            try {
                sleep(500);
            } catch (InterruptedException ex) {
                logOut("Sleep interrupted.  Exception: " + ex.getMessage());
                ex.printStackTrace();
            }
        }

        logOut("Client controller stopped running.");
    }

    void cleanClients() {
        Iterator i = getConnections().iterator();
        try {
            while (i.hasNext()) {
                FileClientConnectionGui c = (FileClientConnectionGui)(i.next());
                if (c.getStatus() == ConnectionStatus.CLOSED) {
                    logOut("Removing dead client at " + c.getInetAddress().toString());
                    tabConnections.remove(c.getMainPanel());
                    removeConnection(c);
                }
            }
        } catch (Exception ex) {
            logOut("cleanClients Exception: " + ex.getMessage());
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

我会得到任何帮助,我会提前感谢您提供的任何建议.我对此深感不安.

也许最让人眼花缭乱的是什么(也许这提供了问题的线索?)是我可以注释掉抽象方法的其他实现(例如runClient,或者connectClient),我没有其他问题,只是相同一.此外,如果我将@Override指令添加到其中一个像这样:

@Override
Connection connectClient(Socket sock) {
    logOut("Client controller is creating a new connection...");
    FileClientConnectionGui c = new FileClientConnectionGui(sock, getLogger(), this);
    addConnection(c);
    c.start();
    logOut("Client controller created a new connection.");
    return c;
}
Run Code Online (Sandbox Code Playgroud)

我收到一个额外的错误:

com/fileclient/FileClientGui.java:96: method does not override or implement a method from a supertype
Run Code Online (Sandbox Code Playgroud)

它显然从其超类型(即客户端)覆盖一个方法.我已经尝试用完整的类路径(lib.client.Client)替换"Client",并且根本没有更改任何错误.

有什么我想念的吗?我不尝试的东西?

Jon*_*eet 9

我相信这是因为你有了包级抽象方法,这些方法在你的子类中是不可见的.尝试让它们受到保护.

这是一对简单的类,可以重现这个问题:

package x1;

public abstract class P1
{
    abstract void foo();
}
Run Code Online (Sandbox Code Playgroud)

然后:

package x2;

public class P2 extends x1.P1
{
    void foo() {}
}
Run Code Online (Sandbox Code Playgroud)

编译它们给出:

P2.java:3: P2 is not abstract and does not override abstract method foo() in P1
public class P2 extends x1.P1
       ^
1 error
Run Code Online (Sandbox Code Playgroud)

使foo这两类保护的解决了这个问题.