<span style="font-size:18px;">package netty.in.action;
/**
* Created by Administrator on 15-4-18.
*/
public class Parent {
public Parent(){
System.out.println("----Parent----");
}
}</span>
<span style="font-size:18px;">package netty.in.action;
/**
* Created by Administrator on 15-4-18.
*/
public class Brother {
public Brother(){
System.out.println("----Brother----");
}
}</span>
<span style="font-size:18px;">package netty.in.action;
/**
* Created by Administrator on 15-4-18.
*/
public class Child extends Parent{
public Child(){
System.out.println("----Child----");
}
Brother b = new Brother();
public static void main(String[] args){
System.out.println(new Child());
}
}</span>
运行结果为:
----Parent----
----Brother----
----Child----
netty.in.action.Child@1267649
本文通过一个简单的Java程序展示了类之间的继承关系及其构造函数的调用顺序。程序定义了Parent、Child和Brother三个类,其中Child类继承自Parent类,并在构造函数中创建了一个Brother类的实例。
5253

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



