有人可以解释一下当我们扩展时同名方法是否会被覆盖吗?
A 类 ab = 新 B 类?
ab 看到哪些方法?
A类:
public class ClassA {
public static int count = 0;
public void print(){
System.out.println("Print A");
}
public static void printNumber() {
System.out.println("Class A " + (++count));
}
}
B 类: B 类中哪些方法将被重写?
public class ClassB extends ClassA {
public static int count=0;
public void print() {
System.out.println("Class B");
}
public static void printNumber() {
System.out.println("Class B " + (++count));
}
}
测试类: 这里会调用哪个方法?
public class ClassTest {
public static void main(String [] arg) {
ClassA a = new ClassA();
ClassB b = new ClassB();
ClassA ab = new ClassB();
a.print();
a.printNumber();
b.print();
b.printNumber();
ab.print(); //Why when we call print() method returns print method of ClassA
ab.printNumber() ;// Why when we call printNumber() method return method of ClassB
}
}
杨__羊羊
相关分类