Java中继承,就是子类拥有父类的,非private的变量和方法。
注意点
- 不能重写final 方法
- 单继承,但可以实现多个接口
- 初始化顺序,先父类再子类
一个例子,可以看到实例化的顺序
class Bird {
public Bird() {
super();
}
public Bird(int i) {
System.out.println("I=" + i);
}
}
class Parent {
static Bird d = new Bird(1);
public Parent() {
System.out.println("Parent constructor");
}
}
public class TestInherit extends Parent {
static Bird d2 = new Bird(2);
public TestInherit() {
System.out.println("Child constructor");
}
public static void main(String[] args) {
new TestInherit();
}
}
执行结果:
I=1
I=2
Parent constructor
Child constructor
本文详细介绍了Java中的继承概念,包括子类如何继承父类的非private成员变量和方法,以及一些重要的注意事项,例如不能重写final方法、Java支持单继承但可以实现多个接口等。通过一个具体的示例代码展示了类实例化的顺序。
11万+

被折叠的 条评论
为什么被折叠?



