The null reference can always be cast to any reference type. If more than one method declaration is both accessible and applicable to a method
invocation, it is necessary to choose one to provide the descriptor for the run-time
method dispatch. The Java programming language uses the rule that the most specific
method is chosen.null 可以cast 到String和Object类型,
而String是Object的子类,所以public void test( String s )方法更specific,
于是调用public void test( String s )然而对于
public void test( StringBuffer sb )
{
System.out.println( "1" );
}
public void test( String s )
{
System.out.println( "2" );
}
来说,
者两个方法没有哪一个比哪一个更specific,编译器不知道选择调用哪个方法,所以出错The informal intuition is that one method declaration is more specific than
another if any invocation handled by the first method could be passed on to the
other one without a compile-time type error.
invocation, it is necessary to choose one to provide the descriptor for the run-time
method dispatch. The Java programming language uses the rule that the most specific
method is chosen.null 可以cast 到String和Object类型,
而String是Object的子类,所以public void test( String s )方法更specific,
于是调用public void test( String s )然而对于
public void test( StringBuffer sb )
{
System.out.println( "1" );
}
public void test( String s )
{
System.out.println( "2" );
}
来说,
者两个方法没有哪一个比哪一个更specific,编译器不知道选择调用哪个方法,所以出错The informal intuition is that one method declaration is more specific than
another if any invocation handled by the first method could be passed on to the
other one without a compile-time type error.
class BB
extends AA // (-------1--------)
{}
class CC
{
public static void main(String[] args) {
new TanXin().test(null);
}
public void test( Object o ) { }
public void test( AA s ){ }
public void test( BB s ){ }
}
上述代码可以编译,最下面的test被调用.因为BB extends AA, which extends Object. 但如果除去-----1----那一行,编译就过不去了,因为AA extends Object, BB extends Object, 不知道用哪个.
解释的很精确哦
method is chosen.呵呵。majcos(千里之行,始于足下)让我知道了真正想要知道的东西。谢谢你。helpall也说的有道理。
我把分给他们两位,大家不介意吧!
非常感谢两位的解答。