• Java中super关键字的作用与用法


    Java中的super是什么?
    java中的super关键字是一个引用变量,用于引用父类对象。关键字“super”以继承的概念出现在类中。主要用于以下情况:

    1.使用super与变量:
    当派生类和基类具有相同的数据成员时,会发生此情况。在这种情况下,JVM可能会模糊不清。我们可以使用以下代码片段更清楚地理解它:

    /* Base class vehicle */
    class Vehicle
    {
        int maxSpeed = 120;
    }
     
    /* sub class Car extending vehicle */
    class Car extends Vehicle
    {
        int maxSpeed = 180;
     
        void display()
        {
            /* print maxSpeed of base class (vehicle) */
            System.out.println("Maximum Speed: " + super.maxSpeed);
        }
    }
     
    /* Driver program to test */
    class Test
    {
        public static void main(String[] args)
        {
            Car small = new Car();
            small.display();
        }
    }

    输出:

    Maximum Speed: 120

    在上面的例子中,基类和子类都有一个成员maxSpeed。我们可以使用super关键字访问sublcass中的基类的maxSpeed。

    2.使用super方法:
    当我们要调用父类方法时使用。所以,无论何时,父类和子类都具有相同的命名方法,那么为了解决歧义,我们使用super关键字。这段代码有助于理解super关键字的使用情况。

    /* Base class Person */
    class Person
    {
        void message()
        {
            System.out.println("This is person class");
        }
    }
     
    /* Subclass Student */
    class Student extends Person
    {
        void message()
        {
            System.out.println("This is student class");
        }
     
        // Note that display() is only in Student class
        void display()
        {
            // will invoke or call current class message() method
            message();
     
            // will invoke or call parent class message() method
            super.message();
        }
    }
     
    /* Driver program to test */
    class Test
    {
        public static void main(String args[])
        {
            Student s = new Student();
     
            // calling display() of Student
            s.display();
        }
    }

    输出:

    This is student class
    This is person class

    在上面的例子中,我们已经看到,如果我们只调用方法message(),那么当前的类message()被调用,但是使用super关键字时,超类的message()也可以被调用。

    3.使用超级构造函数:
    super关键字也可以用来访问父类的构造函数。更重要的是,'超'可以根据情况调用参数和非参数构造函数。以下是解释上述概念的代码片段:

    /* superclass Person */
    class Person
    {
        Person()
        {
            System.out.println("Person class Constructor");
        }
    }
     
    /* subclass Student extending the Person class */
    class Student extends Person
    {
        Student()
        {
            // invoke or call parent class constructor
            super();
     
            System.out.println("Student class Constructor");
        }
    }
     
    /* Driver program to test*/
    class Test
    {
        public static void main(String[] args)
        {
            Student s = new Student();
        }
    }

    输出:

    Person class Constructor
    Student class Constructor

    在上面的例子中,我们通过子类构造函数使用关键字'super'调用超类构造函数。

    其他要点:
    1.调用super()必须是类构造函数中的第一条语句。
    2.如果构造函数没有明确调用超类构造函数,那么Java编译器会自动向超类的无参构造函数插入一个调用。如果超类没有没有参数的构造函数,你会得到一个编译时错误。对象 确实有这样的构造函数,所以如果Object是唯一的超类,那就没有问题了。
    3.如果子类构造函数调用其超类的构造函数,无论是显式还是隐式调用,您都可能认为调用了整个构造函数链,并返回到Object的构造函数。事实上,情况就是如此。它被称为构造函数链。

  • 相关阅读:
    jq获取地址中的参数的值
    移动设备分辨率
    随机生成颜色
    小程序轮播图
    JavaScript中的constructor、instanceof、isPrototypeOf、typeof以及hasOwnProperty
    call()和apply()函数
    行内元素和块级元素
    js跳出循环的方法区别(break,continue,return)
    js 的四种设计模式的优缺点
    常见浏览器兼容性问题与解决方案
  • 原文地址:https://www.cnblogs.com/xiaxue168/p/10468667.html
Copyright © 2020-2023  润新知