publicclassTestMain{publicstaticvoidmain(String[]args){Stringstr=newString();Callercaller=newCaller();caller.call(str);}staticclassCaller{publicvoidcall(Objectobj){System.out.println("an Object instance in Caller");}publicvoidcall(Stringstr){System.out.println("a String instance in in Caller");}}}
publicclassTestMain{publicstaticvoidmain(String[]args){Stringstr=newString();Callercaller=newSubCaller();caller.call(str);}staticclassCaller{publicvoidcall(Stringstr){System.out.println("a String instance in Caller");}}staticclassSubCallerextendsCaller{@Overridepublicvoidcall(Stringstr){System.out.println("a String instance in SubCaller");}}}
publicclassTestMain{publicstaticvoidmain(String[]args){Stringstr=newString();CallercallerSub=newSubCaller();callerSub.call(str);}staticclassCaller{publicvoidcall(Objectobj){System.out.println("an Object instance in Caller");}publicvoidcall(Stringstr){System.out.println("a String instance in in Caller");}}staticclassSubCallerextendsCaller{@Overridepublicvoidcall(Objectobj){System.out.println("an Object instance in SubCaller");}@Overridepublicvoidcall(Stringstr){System.out.println("a String instance in in SubCaller");}}}
执行结果为
12
22:30$javaTestMainaStringinstanceininSubCaller
验证
由于上面已经介绍,这里只贴一下反编译结果啦
123456789101112131415161718192021222324
22:30$javap-cTestMainCompiledfrom"TestMain.java"publicclassTestMain{publicTestMain();Code:0:aload_01:invokespecial#1// Method java/lang/Object."<init>":()V4:returnpublicstaticvoidmain(java.lang.String[]);Code:0:new#2// class java/lang/String3:dup4:invokespecial#3// Method java/lang/String."<init>":()V7:astore_18:new#4// class TestMain$SubCaller11:dup12:invokespecial#5// Method TestMain$SubCaller."<init>":()V15:astore_216:aload_217:aload_118:invokevirtual#6// Method TestMain$Caller.call:(Ljava/lang/String;)V21:return}
staticclassSuperCaller{publicvoidcall(Objectobj){System.out.println("an Object instance in SuperCaller");}}staticclassBaseCallerextendsSuperCaller{publicvoidcall(Objectobj){System.out.println("an Object instance in BaseCaller");}}
publicclassTestMain{publicstaticvoidmain(String[]args){Objectobj=newObject();SuperCallercallerSub=newSubCaller();callerSub.call(obj);}staticclassCallerextendsBaseCaller{publicvoidcall(Objectobj){System.out.println("an Object instance in Caller");super.call(obj);}publicvoidcall(Stringstr){System.out.println("a String instance in in Caller");}}staticclassSubCallerextendsCaller{@Overridepublicvoidcall(Objectobj){System.out.println("an Object instance in SubCaller");}@Overridepublicvoidcall(Stringstr){System.out.println("a String instance in in SubCaller");}}}