[英]Ambiguous call to constructor “super(null)” in java.net.DatagramSocket?
我正在扩展Java.net.DatagramSocket
类,并为子类提供自己的构造函数。 我的新类UDP_Socket
的构造函数如下
private UDP_Socket(int port) throws IOException {
super(null); // Creates an unbound socket.
super.setReuseAddress(true); // You set reuse before binding.
super.bind(new InetSocketAddress(port)); // finally, bind
}
唯一的问题是在super(null)
,我ambiguous reference to both constructor DatagramSocket(DatagramSocketImpl) and DatagramSocket(SocketAddress)
。 我需要super(null)
行来创建未绑定的套接字,并且我相信应该在绑定之前设置重用地址套接字选项。 如何解决此错误?
问题是因为您调用super(null)
是DatagramSocket(DatagramSocketImpl)
和DatagramSocket(SocketAddress)
有效参数。
因此,编译器感到困惑。 您只能调用其中一个构造函数。
采用,
super((DatagramSocketImpl) null);
要么,
super((SocketAddress) null);
取决于要调用的构造函数。
错误:
ambiguous reference to both constructor DatagramSocket(DatagramSocketImpl) and DatagramSocket(SocketAddress)
告诉您null
可以引用(DatagramSocketImpl) null
或(SocketAddress) null
从而使其模棱两可。
您必须显式调用两个构造函数之一
// super((DatagramSocketImpl) null)
super((SocketAddress) null)
或者让您的构造函数要求
private UDP_Socket(int port, SocketAddress addr) throws IOException {
super(addr);
编辑:
来自以下代码的代码: 类DatagramSocket
private DatagramSocket getDatagramSocket (int port) {
DatagramSocket s = new DatagramSocket(null);
s.setReuseAddress(true);
s.bind(new InetSocketAddress(port));
return s;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.