【发布时间】:2018-11-28 17:24:17
【问题描述】:
我有另一个示例程序,它确实覆盖但所有方法都有相同数量的参数。
class A {
int a;
// function with dummy parameters
void printArray(int i) {
System.out.println("A");
}
}
class B extends A {
//function with dummy parameters
void printArray(int i, int s) {
System.out.println("B");
}
}
public class JavaApplication5 {
public static void main(String[] args) {
A ob = new A();
B o2 = new B();
A o3;
o3 = o2;
o3.printArray(3, 2); // It says that it can not be applied to given type :(
}
}
【问题讨论】:
-
如果您不关心问题的格式,就不要期待答案
-
Java 类型系统是静态的。
o3是A类型,因此它无法访问两个参数的方法。 -
您的代码中根本没有发生覆盖。您只是想在 A 类型的变量上调用仅在 B 类中定义的方法。
标签: java inheritance