任何人都可以提供一个简单的例子来解释Java中动态和静态多态的区别吗?
我最近遇到了两个重载问题,我找不到答案,也没有java环境来运行一些测试代码.我希望有人可以通过汇编一份java编译器遵循的所有规则列表来帮助我进行重载,或者交替指向我已经存在的列表.
首先,当两个方法只有最终的varargs参数不同时,在什么情况下每个方法都被调用,你可以在没有任何args的情况下调用varargs方法吗?
private void f(int a) { /* ... */ }
private void f(int a, int... b) { /* ... */ }
f(12); // calls the former? I would expect it to
f(12, (int[])null); // calls latter, but passes null for b?
// Can I force the compiler to call the second method in the same fashion
// as would happen if the first method didn't exist?
Run Code Online (Sandbox Code Playgroud)
第二个问题,当两个方法因为彼此继承而被调用的类型不同时?我希望调用最多的派生版本,并允许调用另一个版本.
interface A {}
class B implements A {}
class C implements A …Run Code Online (Sandbox Code Playgroud)