请考虑以下示例代码:
public class TestClass {
public void doSth(String str, String l, Object... objects) {
System.out.println("A");
}
public void doSth(String str, Object... objects) {
System.out.println("B");
}
}
Run Code Online (Sandbox Code Playgroud)
当我现在打电话时,new TestClass().doSth("foo", "bar")我得到了预期的结果A.但是如果我通过将参数l变为基本类型来更改第一个方法的方法签名:
public class TestClass {
public void doSth(String str, long l, Object... objects) {
System.out.println("A");
}
public void doSth(String str, Object... objects) {
System.out.println("B");
}
}
Run Code Online (Sandbox Code Playgroud)
调用new TestClass().doSth("foo", 2L)将产生reference to call ambiguous编译时错误.
我现在考虑了那个问题,并且还咨询了这个stackoverflow问题,但我无法理解为什么会发生这种情况.在我看来,签名doSth("foo", 2L)更具体doSth(String string, …
我有一个marshaller的以下界面,它将Object一个DataNode对象和一个对象组合在一起:
public interface DataMarshaller<T> {
/**
* returns the actual class that this marshaller is capable of marshalling.
* @return
*/
Class<T> getDataClass();
/**
* marshalls the object to a DataNode
*
* @param object
* @return
*/
DataNode marshal(T object);
/**
* unmarshalls the object from a DataNode
*
* @param node
* @return
*/
T unMarshal(DataNode node);
}
Run Code Online (Sandbox Code Playgroud)
为了确保我能够为对象获取正确的编组器,我还有Class<T> getDataClass()返回其类的方法(由于类型擦除而在编译后会丢失).
现在我想在一个能够编组类型对象的类中实现这个接口Octree<T>:
public class OctreeMarshaller<T> implements DataMarshaller<Octree<T>> {
@Override
public Class<Octree<T>> …Run Code Online (Sandbox Code Playgroud)