zoukankan      html  css  js  c++  java
  • 课堂动手动脑

        关于在继承中的super()

    在继承中,如果想要调用父类的构造方法,那么我们就一定要用到super()函数,具体用法如下:

    class animals
    {
    public animals()
    {
    System.out.println("I am an animal .");
    }
    public animals(String s)
    {
    System.out.println("I am a" + s);
    }
    }
    class terrestrial extends animals
    {
    public terrestrial()
    {
    super("terrestial");
    System.out.println("I am a terrestial animals .");
    }
    }
    class lion extends terrestrial
    {
    public lion()
    {
    System.out.println("I am a lion .");
    }
    }
    public class Program_2 {
    public static void main(String args[])
    {
    lion l = new lion();
    }

    }

    在这段代码中,我们可以清晰地发现我们能够调用父类中的同名构造方法,具体的运行结果如下:

                                                                  

    我们可以发现我们成功地调用了父类animals的构造函数。

  • 相关阅读:
    Gym
    HDU
    HDU
    POJ
    洛谷P3690 Link Cut Tree (动态树)
    Gym
    P4294 [WC2008]游览计划 (斯坦纳树)
    洛谷P3264 [JLOI2015]管道连接 (斯坦纳树)
    HDU
    Controller调试接口
  • 原文地址:https://www.cnblogs.com/overs/p/6044586.html
Copyright © 2011-2022 走看看