我会尽可能简明扼要,但这是一个复杂的问题。我正在 Linux 平台上用 Java 编写,无论它值多少钱。
目标的简短版本:我想要一个名为 Client 的抽象类,它充当客户端连接的通用容器。 Client 应该线程化它的每个连接。我也有一些半测试代码,以类似的编码方式播放与此对应的服务器。抽象的 Client 应该被实现为更具体和可实例化的东西。在我的例子中,我有一个名为 FileClientGui 的类,它扩展了 Client 并用接收从服务器获取文件的内容并显示它们。由于抽象的 Client 本身是 java.lang.Thread 的扩展,这一点变得更加复杂。
所以这是我的通用术语的文件结构:
/class/path/lib/client/Client.java
/class/path/com/fileclient/FileClientGui.java
这两个文件都引用了其他几个自定义类,但我没有从中发现任何错误。如果我需要发布这些项目的代码,请告诉我,我会发布它们。
因此,我在终端上运行了这条长长的 javac 命令,设置了类路径和构建目录以及所有需要编译的相关文件。我收到的任何该代码的唯一错误是:
com/fileclient/FileClientGui.java:26: com.fileclient.FileClientGui is not abstract and does not override abstract method cleanClients() in lib.client.Client
我的代码(见下文)清楚地实现了 Client.java 中定义的方法和所有其他抽象方法。我搜索了 Internet,似乎大多数遇到此错误的人都在尝试执行类似 ActionListener 的操作,并对该实现感到困惑,很多时候,这只是一个简单的拼写或大写问题.我一遍又一遍地检查我的代码,以确保这不是一个像那样简单的“糟糕”问题。我怀疑这实际上是我的类的名称与其他类的名称之间的某种冲突,以某种方式最终出现在我的类路径或 Java 的 native 框架/库中,但我找不到任何明显的东西。
无论如何,这是我的代码。
客户端.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();
}
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());
}
}
}
我会竭尽所能,并提前感谢您提供的任何建议。我对此感到非常困惑。
也许最令人困惑的是(也许这提供了问题的线索?)是我可以注释掉抽象方法的其他实现(例如 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;
}
我收到一个额外的错误:
com/fileclient/FileClientGui.java:96: method does not override or implement a method from a supertype
它显然正在覆盖其父类(super class)型(即Client)的方法。我已经尝试用完整的类路径 (lib.client.Client) 替换“Client”,但所有错误都没有改变。
有什么我想念的吗?我没有尝试的东西?
最佳答案
我相信这是因为您拥有包级抽象方法,这些方法在您的子类中是不可见的。尝试让它们受到保护。
这里有一对简单的重现问题的类:
package x1;
public abstract class P1
{
abstract void foo();
}
然后:
package x2;
public class P2 extends x1.P1
{
void foo() {}
}
编译它们给出:
P2.java:3: P2 is not abstract and does not override abstract method foo() in P1
public class P2 extends x1.P1
^
1 error
使 foo 在两个类中都受到保护可以解决这个问题。
关于javac 声称我没有覆盖抽象类实现中的方法,而我显然是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3200174/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr