0

私はscala(初心者)で遊んでいて、Java 7 NIOを使おうとしていました(簡単に始めたいので)。しかし、受け入れのためにCompletionHandlerをインスタンス化する方法がわかりません。次のコードは間違っており、修正できません。

package async

import java.nio.channels.AsynchronousServerSocketChannel
import java.net.InetAddress
import java.net.InetSocketAddress
import java.nio.channels.CompletionHandler
import java.nio.channels.AsynchronousSocketChannel

class AsyncServer (port: Int) {

  val socketServer = AsynchronousServerSocketChannel.open();
  socketServer.bind(new InetSocketAddress(port))

  val connectionHandler = new CompletionHandler[AsynchronousSocketChannel, Integer](){

  }

  def init() = socketServer accept(1 ,  connectionHandler)

}
4

1 に答える 1

2

インスタンスを作成するには、メソッドconnectHandlerを実装する必要があります。CompletionHandler

...
val connectionHandler = new CompletionHandler[AsynchronousSocketChannel, Integer] {
  def completed(result: AsynchronousSocketChannel, attachment: Integer ) {}
  def failed(exc: Throwable , attachment: Integer) {}
}
...

また、インターフェイスの型は A では不変ですが、呼び出しているメソッドは A では反変であるためです。

...
public abstract <A> void accept(A attachment,
                                CompletionHandler<AsynchronousSocketChannel,? super A> handler);
...

タイプをチェックするには、キャストする必要があります。

socketServer accept(1, connectionHandler.asInstanceOf[CompletionHandler[java.nio.channels.AsynchronousSocketChannel, _ >: Any]]
于 2013-10-02T22:19:09.177 に答える