- class A {
-
- public void show() {
- show2();
- }
- public void show2() {
- System.out.println("我");
- }
- }
- class B extends A {
- public void show() {
- show2();
- }
-
- public void show2() {
- System.out.println("爱");
- }
- }
- class C extends B { //多层继承
- public void show() {
- super.show();
- }
-
- public void show2() { //为什么这个方法不写也不影响程序运行结果呢?
- System.out.println("你");
- }
- }
- public class Test2DuoTai {
- public static void main(String[] args) {
-
- A a = new B(); //A父类引用指向B子类对象
- a.show(); //爱
-
- B b = new C(); //B父类引用指向C子类对象
- b.show(); //你
-
- }
- }
复制代码
|
|