Java 可调用类给出错误:掺杂不是抽象的,并且不重写抽象方法调用()?
在尝试创建可调用类时,我似乎遇到了上述错误。我已经找过原因了,但似乎什么也找不到。NetBeans为我提供了一些使事情抽象化的选项,但我对这一点还不熟悉,我更愿意找出事情发生的原因。有人能解释一下吗Java 可调用类给出错误:掺杂不是抽象的,并且不重写抽象方法调用()?,java,multithreading,callable,Java,Multithreading,Callable,在尝试创建可调用类时,我似乎遇到了上述错误。我已经找过原因了,但似乎什么也找不到。NetBeans为我提供了一些使事情抽象化的选项,但我对这一点还不熟悉,我更愿意找出事情发生的原因。有人能解释一下吗 public class doPing implements Callable<String>{ public String call(String IPtoPing) throws Exception{ String pingOutput = null;
public class doPing implements Callable<String>{
public String call(String IPtoPing) throws Exception{
String pingOutput = null;
//gets IP address and places into new IP object
InetAddress IPAddress = InetAddress.getByName(IPtoPing);
//finds if IP is reachable or not. a timeout timer of 3000 milliseconds is set.
//Results can vary depending on permissions so cmd method of doing this has also been added as backup
boolean reachable = IPAddress.isReachable(1400);
if (reachable){
pingOutput = IPtoPing + " is reachable.\n";
}else{
//runs ping command once on the IP address in CMD
Process ping = Runtime.getRuntime().exec("ping " + IPtoPing + " -n 1 -w 300");
//reads input from command line
BufferedReader in = new BufferedReader(new InputStreamReader(ping.getInputStream()));
String line;
int lineCount = 0;
while ((line = in.readLine()) != null) {
//increase line count to find part of command prompt output that we want
lineCount++;
//when line count is 3 print result
if (lineCount == 3){
pingOutput = "Ping to " + IPtoPing + ": " + line + "\n";
}
}
}
return pingOutput;
}
}
公共类实现可调用{
公共字符串调用(字符串IPtoPing)引发异常{
字符串pingOutput=null;
//获取IP地址并放入新的IP对象
InetAddress IPAddress=InetAddress.getByName(IPtoPing);
//查找IP是否可访问。设置了3000毫秒的超时计时器。
//根据权限的不同,结果可能会有所不同,因此执行此操作的cmd方法也已添加为备份
布尔可及=IPAddress.isRecable(1400);
如果(可到达){
pingOutput=IPtoPing+“是可访问的。\n”;
}否则{
//在CMD中的IP地址上运行ping命令一次
进程ping=Runtime.getRuntime().exec(“ping”+IPtoPing+“-n1-w300”);
//从命令行读取输入
BufferedReader in=新的BufferedReader(新的InputStreamReader(ping.getInputStream());
弦线;
int lineCount=0;
而((line=in.readLine())!=null){
//增加行数以查找所需的部分命令提示符输出
lineCount++;
//当行数为3时,打印结果
如果(行数==3){
pingOutput=“Ping到“+IPtoPing+”:“+line+”\n”;
}
}
}
返回输出;
}
}
您的“兴奋剂”类定义为实现可调用的
。这意味着它应该实现不接受任何参数的call()
方法。以下是可调用的的定义:
public interface Callable<V> {
V call() throws Exception;
}
public class doPing implements Callable<String> {
// you need to define this method with no arguments to satisfy Callable
public String call() throws Exception {
...
}
// this method does not satisfy Callable because of the IPtoPing argument
public String call(String IPtoPing) throws Exception {
...
}
}
您的“兴奋剂”类定义为实现可调用的。这意味着它应该实现不接受任何参数的call()
方法。以下是可调用的的定义:
public interface Callable<V> {
V call() throws Exception;
}
public class doPing implements Callable<String> {
// you need to define this method with no arguments to satisfy Callable
public String call() throws Exception {
...
}
// this method does not satisfy Callable because of the IPtoPing argument
public String call(String IPtoPing) throws Exception {
...
}
}
该接口要求您有一个call()
方法。然而,您的方法是
呼叫(字符串ipToPing)
考虑添加一个setIpToPing(stringip)
方法来设置正确的ip
即
该接口要求您有一个call()
方法。然而,您的方法是
呼叫(字符串ipToPing)
考虑添加一个setIpToPing(stringip)
方法来设置正确的ip
即
在您的代码中,call
方法有一个参数:它不会覆盖Callable
接口的call
方法-它应该如下所示:
public String call() throws Exception{ //not public String call(String IPtoPing)
}
如果您使用的是Java 6+,最好使用Override
注释,这有助于发现错误的方法签名(在这种情况下,您已经遇到编译错误):
在您的代码中,call
方法有一个参数:它不会覆盖Callable
接口的call
方法-它应该如下所示:
public String call() throws Exception{ //not public String call(String IPtoPing)
}
如果您使用的是Java 6+,最好使用Override
注释,这有助于发现错误的方法签名(在这种情况下,您已经遇到编译错误):
谢谢你。非常感谢。IPtoPing是另一个类中我的main方法中的一个变量,因此我如何将该变量带到call()?@user1286779没有问题。我添加了一个示例。谢谢。非常感谢。IPtoPing是另一个类中我的main方法中的一个变量,因此我如何将该变量带到call()?@user1286779没有问题。我添加了一个示例。