在java中,预先定义‘super’这个词与父类相关。如果我们需要一气呵成的话,那么java中的super关键字是指处理父类对象,而super()则处理父类构造函数。我们将首先讨论超级关键字的概念,然后讨论super()。
概念:超级关键字
java中的super关键字是一个引用变量,用于引用父类对象。关键词“super” 伴随着继承的概念而出现。本质上是这种形式极好的当超类中没有构造函数时,用于初始化超类变量。另一方面,它通常用于访问超类的特定变量。
示例
Java
// Java Program to Illustrate super keyword
// Class 1
// Base class
// Here it is vehicle class
class Vehicle {
    // Attribute
    int maxSpeed = 120;
}
// Class 2
// sub class Car extending vehicle
class Car extends Vehicle {
    int maxSpeed = 180;
    // Method
    void display()
    {
        // Printing maxSpeed of parent class (vehicle) as
        // super keyword refers to parent class
        System.out.println("Maximum Speed: "
                           + super.maxSpeed);
    }
}
// Class 3
// Main class
class GFG {
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of child class
        Car small = new Car();
        // Calling out method defined inside child class
        small.display();
    }
}输出
Maximum Speed: 120
现在我们已经看到 super 关键字指的是父类,这可以从输出本身看出。现在让我们详细讨论第二个概念super(),更多的程序员不太了解这个概念,并且在几乎不知道这一切的情况下不实现相同的概念
概念:super()
super 关键字还可以通过在其后添加“()”来访问父类构造函数,即super()。另请记住,“super()”可以根据情况调用参数和非参数构造函数。
例子:
Java
// Java code to demonstrate super()
// Class 1
// Helper class
// Parent class - Superclass
class Person {
    // Constructor of superclass
    Person()
    {
        // Print statement of this class
        System.out.println("Person class Constructor");
    }
}
// Class 2
// Helper class
// Subclass extending the above  superclass
class Student extends Person {
    Student()
    {
        // Invoking the parent class constructor
        // with the usage of super() word
        super();
        // Print styatement whenever subclass constructor is
        // called
        System.out.println("Student class Constructor");
    }
}
// Class 3
// Main class
class GFG {
    // Main driver method
    public static void main(String[] args)
    {
        // Creating object of subclass
        // inside main() method
        Student s = new Student();
    }
}输出:
Person class Constructor Student class Constructor
最后,在对上述主题有了充分的了解之后,让我们最终总结出它们之间的差异,并以下面的表格形式列出:
| 极好的 | super() | 
|---|---|
| Java中的super关键字是一个引用变量,用于引用父类对象。 | Java中的super()是一个引用变量,用于引用父类构造函数。 | 
| super可以用来调用父类的变量和方法。 | super()只能用于调用父类的构造函数。 | 
| 通过super关键字可以随时调用变量和方法, | 对 super() 的调用必须是 Derived(Student) 类构造函数中的第一个语句。 | 
| 如果没有使用 super 关键字显式调用超类变量或方法,则不会发生任何事情 | 如果构造函数未使用 super() 显式调用超类构造函数,则 Java 编译器会自动插入对超类的无参构造函数的调用。 | 
相关用法
- Java super()和this()的区别用法及代码示例
- Java stream.limit()用法及代码示例
- Java streams counting()用法及代码示例
- Java sqrt()用法及代码示例
- Java signum()用法及代码示例
- Java string转boolean用法及代码示例
- Java sleep和yield的区别用法及代码示例
- Java String compareToIgnoreCase()用法及代码示例
- Java String compareTo()用法及代码示例
- Java String split()用法及代码示例
- Java String length()用法及代码示例
- Java String replace()用法及代码示例
- Java String replaceAll()用法及代码示例
- Java String substring()用法及代码示例
- Java String equals()用法及代码示例
- Java String equalsIgnoreCase()用法及代码示例
- Java String contains()用法及代码示例
- Java String indexOf()用法及代码示例
- Java String trim()用法及代码示例
- Java String charAt()用法及代码示例
- Java String toLowerCase()用法及代码示例
- Java String concat()用法及代码示例
- Java String valueOf()用法及代码示例
- Java String matches()用法及代码示例
- Java String startsWith()用法及代码示例
注:本文由纯净天空筛选整理自SohelRaja大神的英文原创作品 Difference between super and super() in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
