继承

来源:互联网 发布:ubuntu 系统剪贴板 编辑:程序博客网 时间:2024/06/06 23:19


class Child {
public void f() {
System.out.println("Child.f()");
g();
h();
i();
j();
}


protected void g() {
System.out.println("Child.g()");
}


private void h() {
System.out.println("Child.h()");
}


void i() {
System.out.println("Child.i()");
}


public void j() {
System.out.println("Child.j()");
}


public void k() {
System.out.println("Child.k()");
}
}


class Father extends Child {
public void f() {
System.out.println("Father.f()");
super.f();
}


protected void g() {
System.out.println("Father.g()");
}


private void h() {
System.out.println("Father.h()");
}


public void i() {
System.out.println("Father.i()");
}


public void j() {
System.out.println("Father.j()");
}
}


public class Override {
public static void main(String[] args) {
Child b = new Father();
b.f();
b.g();
b.i();
b.j();
b.k();
}
}

这里先调用子类的f(),然后通过super()调用父类的f(),

然后父类f()中又调用g(),h(),i(),j(),k()方法

可以发现除了private修饰的h()方法,其余的方法都是调用子类覆盖的方法。

而没被覆盖的k()方法就是直接输出。

输出:

Father.f()
Child.f()
Father.g()
Child.h()
Father.i()
Father.j()
Father.g()
Father.i()
Father.j()
Child.k()