先父类再子类
package com.mmzs.controller;
class BaseClass {
public BaseClass() { //父类构造方法
System.out.println("调用父类构造方法");
init();
}
protected void init() {
System.out.println("do init");
}
}
public class test extends BaseClass{
public test() { //子类构造方法
System.out.println("调用 子类构造方法");
}
protected void init() {//重写父类的init方法
assert this != null;
System.out.println("now the working class is:" + this.getClass().getSimpleName());
System.out.println("in SubClass");
}
public static void main(String[] args)
{
new test();
}
}
//运行结果:
调用父类构造方法
now the working class is:test
in SubClass
调用 子类构造方法
给父类增加一没用的构造函数 public class Parent { public Parent() { Console.WriteLine("parent"); } public Parent(int i){} } public class Child:Parent { public Child():base(0)//继承父类的Parent(int i)这个空构造函数 { Console.WriteLine("child"); } }