1>Java会自动在导出类的构造器中插入其基类构造器的调用

1.1无参的情况

  1. class Art{
  2. Art(){
  3. System.out.println("Art()");
  4. }
  5. }
  6. class Drawing extends Art{
  7. Drawing(){
  8. System.out.println("Drawing()");
  9. }
  10. }
  11. public class Cartoon extends Drawing{
  12. //在导出类中包含对父类的一个引用
  13. Cartoon(){
  14. System.out.println("Cartoon()");
  15. }
  16. public static void main(String[] args) {
  17. new Cartoon();
  18. }
  19. }

1.2有参的情况:

当基类和子类均为有参构造器的时候,必须得在子类中显示调用父类得构造器,否则报错;如果基类中有无参构造器,子类可以不用显式的调用基类的构造器。

  1. class Game{
  2. Game(int i ){
  3. System.out.println("Game(i)");
  4. }
  5. }
  6. class BoardGame extends Game{
  7. //当基类和子类为有参构造器的时候,必须得在子类中显示调用父类得构造器
  8. BoardGame(int i){
  9. super(i);
  10. System.out.println("Boardgame(i)");
  11. }
  12. }
  13. public class Chess extends BoardGame{
  14. Chess() {
  15. super(11);
  16. }
  17. }