以下是overload的一段代码,如果能给出运行结果,并能讲出原因说明了解了.
public
class
OverloadTest
{
void test(Object s) {
System.out.println("Object version");
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
void test(OverloadTest s) {
System.out.println("OverloadTest version");
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
void test(SubOverloadTest s) {
System.out.println("SubOverloadTest version");
}
void test(String s) {
System.out.println ("String version");
}
public static void main (String args[]) {
OverloadTest c = new OverloadTest ();
c.test (null);
}
}
结果是编译通不过,提供:The method test(Object) is ambiguous for the type OverloadTest.
如果把:
void test(String s){
System.out.println ("String version");
}
注释,编译通过运行结果为:SubOverloadTest version.
也就是说java的overload时参数在单根继承链时,调用时会选用最特殊类型参数的函数.而且调用的方法的参数不能同时满足两个同级别的类型值.比如:
void test(SubOverloadTest s) {
System.out.println("SubOverloadTest version");
}
void test(String s){
System.out.println ("String version");
}
调用 new OverloadTest (null)时,null同时既是Oject类型又是String类型,编译就会通不过.如果是这样调用 new OverloadTest (new OverloadTest()),就能编译通过,运行结果为:OverloadTest version.
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/717446ca04a6125dc5b6b54e0fa14ab4.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/717446ca04a6125dc5b6b54e0fa14ab4.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/717446ca04a6125dc5b6b54e0fa14ab4.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/717446ca04a6125dc5b6b54e0fa14ab4.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/717446ca04a6125dc5b6b54e0fa14ab4.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/0196c3df5ea9e936f21e9932cca91014.gif)
结果是编译通不过,提供:The method test(Object) is ambiguous for the type OverloadTest.
如果把:
void test(String s){
System.out.println ("String version");
}
注释,编译通过运行结果为:SubOverloadTest version.
也就是说java的overload时参数在单根继承链时,调用时会选用最特殊类型参数的函数.而且调用的方法的参数不能同时满足两个同级别的类型值.比如:
void test(SubOverloadTest s) {
System.out.println("SubOverloadTest version");
}
void test(String s){
System.out.println ("String version");
}
调用 new OverloadTest (null)时,null同时既是Oject类型又是String类型,编译就会通不过.如果是这样调用 new OverloadTest (new OverloadTest()),就能编译通过,运行结果为:OverloadTest version.