0

呼び出し可能なクラスを作成しようとすると、上記のエラーが発生するようです。理由を調べてみましたが、何も見つからないようです。NetBeans は物事を抽象化するためのいくつかのオプションを提供してくれますが、私はこれが初めてで、何かが起こっている理由を知りたいと思っています。誰でもこれに光を当てることができますか?

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;
    }
}
4

3 に答える 3

2

コードでは、メソッドに引数があります。インターフェイスのメソッドをcallオーバーライドしません。次のようになります。callCallable

public String call() throws Exception{ //not public String call(String IPtoPing)

}

Java 6 以降を使用している場合は、Overrideアノテーションを使用することをお勧めします。これは、間違ったメソッド シグネチャを見つけるのに役立ちます (この場合、既にコンパイル エラーが発生しています)。

@Override
public String call() throws Exception{
}
于 2012-04-02T14:55:35.893 に答える
1

「doPing」クラスは次のように定義されていimplements Callable<String>ます。これは、引数を取らないcall()メソッドを実装する必要があることを意味します。の定義は次のとおりです。Callable

public interface Callable<V> {
    V call() throws Exception;
}

String IPtoPingしたい場合は、引数を削除する必要がありdoPingますCallable

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 {
         ...
     }
}
于 2012-04-02T14:53:34.773 に答える
1

Callableインターフェースには、メソッドが必要ですcall()。しかし、あなたの方法は
call(String ipToPing).

setIpToPing(String ip)正しい IP を設定する方法を追加することを検討してください。

いえ

doPing myCallable = new doPing();//Note doPing should be called DoPing to keep in the java naming standards.
myCallable.setIpToString(ip);//Simple setter which stores ip in myCallable
myCallable.call();
于 2012-04-02T14:54:58.940 に答える