zoukankan      html  css  js  c++  java
  • super 要点


    class Grandparent
    {
        public Grandparent()
        {
            System.out.println("GrandParent Created.");
        }
        public Grandparent(String string)
        {
            System.out.println("GrandParent Created.String:"+string);
        }
    }
    class Parent extends Grandparent
    {
        public Parent()
        {
            super("Hello.Grandparent.");
            System.out.println("Parent Created");
            //super("Hello.Grandparent.");
        }
    }
    class Child extends Parent
    {
        public Child()
        {
            System.out.println("Child Created");
        }
    }
    public class TestInherists {
        public static void main(String args[])
        {
            Child c=new Child();
        }
    }

    结果截图:

    若把上面绿色注释取消,如下

    class Parent extends Grandparent
    {
        public Parent()
        {
            
            System.out.println("Parent Created");
            super("Hello.Grandparent.");
        }
    }

    就会报错

    原因:通过super调用基类构造方法,必须是子类构造方法中的第一个语句

    this 和 super有区别,this用于此类,而super用于父类,若没写super,系统将默认进行super,若写了,则必须放在第一个语句

  • 相关阅读:
    SQL学习
    设计模式学习之简单工厂
    c#读写操作3
    SQL存储过程学习
    c# xml的读写
    SQL存储过程实例
    存储过程分页
    搞双显示器
    转:用药的七种心理误区
    lp提了一个非常让偶非常郁闷的要求……
  • 原文地址:https://www.cnblogs.com/cchjl/p/4948127.html
Copyright © 2011-2022 走看看