2024-12-14 08:00:05

Java中的super()

super()用于调用父构造函数吗? 请解释super()。


当前回答

The super keyword in Java is a reference variable that is used to refer to the immediate parent class object.

Java超级关键字的使用

Super可用于引用直接父类实例变量。 Super可用于调用直接父类方法。 Super()可用于调用直接父类构造函数。

其他回答

一些事实:

Super()用于调用直接父节点。 Super()可以用于实例成员,即实例变量和实例方法。 可以在构造函数中使用Super()来调用父类的构造函数。

好,现在让我们实际实现super()的这些点。

检查程序1和程序2之间的区别。这里,程序2证明了Java中super()的第一个语句。

程序1

class Base
{
    int a = 100;
}

class Sup1 extends Base
{
    int a = 200;
    void Show()
    {
        System.out.println(a);
        System.out.println(a);
    }
    public static void main(String[] args)
    {
        new Sup1().Show();
    }
}

输出:

200 200

现在检查程序2,试着找出主要的区别。

项目2

class Base
{
    int a = 100;
}

class Sup2 extends Base
{
    int a = 200;
    void Show()
    {
        System.out.println(super.a);
        System.out.println(a);
    }
    public static void main(String[] args)
    {
        new Sup2().Show();
    }
}

输出:

100 200

在程序1中,输出只是派生类。它既不能打印基类的变量,也不能打印父类的变量。但是在程序2中,我们在输出变量a时使用了super(),它打印的不是派生类变量a的值,而是基类变量a的值。因此,它证明了super()是用来调用直接父节点的。

好,现在看看程序3和程序4的区别。

项目3

class Base
{
    int a = 100;
    void Show()
    {
        System.out.println(a);
    }
}

class Sup3 extends Base
{
    int a = 200;
    void Show()
    {
        System.out.println(a);
    }
    public static void Main(String[] args)
    {
        new Sup3().Show();
    }
}

输出:

200

这里输出是200。当我们调用Show()时,派生类的Show()函数被调用。但是,如果我们想调用父类的Show()函数,应该怎么做呢?请查看程序4以获得解决方案。

项目4

class Base
{
    int a = 100;
    void Show()
    {
        System.out.println(a);
    }
}

class Sup4 extends Base
{
    int a = 200;
    void Show()
    {
        super.Show();
        System.out.println(a);
    }
    public static void Main(String[] args)
    {
        new Sup4().Show();
    }
}

输出:

100 200

这里我们有两个输出,100和200。当调用派生类的Show()函数时,它首先调用父类的Show()函数,因为在派生类的Show()函数内部,我们通过将super关键字放在函数名之前调用父类的Show()函数。

Super()不带参数地调用父构造函数。

它也可以与参数一起使用。例如super(argument1),它将调用接受一个argument1类型形参的构造函数(如果存在的话)。

它还可以用于从父类调用方法。即super.aMethod ()

更多信息和教程在这里

来源文章:Java:调用super()


是的。Super(…)将调用超类的构造函数。

说明:

独立的例子:

class Animal {
    public Animal(String arg) {
        System.out.println("Constructing an animal: " + arg);
    }
}

class Dog extends Animal {
    public Dog() {
        super("From Dog constructor");
        System.out.println("Constructing a dog.");
    }
}

public class Test {
    public static void main(String[] a) {
        new Dog();
    }
}

打印:

Constructing an animal: From Dog constructor
Constructing a dog.

构造函数 在构造函数中,可以使用它而不带点来调用另一个构造函数。Super调用父类中的构造函数;调用该类中的构造函数:

public MyClass(int a) {
  this(a, 5);  // Here, I call another one of this class's constructors.
}

public MyClass(int a, int b) {
  super(a, b);  // Then, I call one of the superclass's constructors.
}

Super在父类需要初始化自身时很有用。这允许您只在一个构造函数中编写一次所有硬初始化代码,并从所有其他更容易编写的构造函数调用它,这很有用。

方法 在任何方法中,都可以使用它和一个点来调用另一个方法。Super.method()调用超类中的方法;this.method()调用该类中的一个方法:

public String toString() {
  int    hp   = this.hitpoints();  // Calls the hitpoints method in this class
                                   //   for this object.
  String name = super.name();      // Calls the name method in the superclass
                                   //   for this object.

  return "[" + name + ": " + hp + " HP]";
}

super在某些情况下是有用的:如果你的类和你的超类有相同的方法,Java会假设你想在你的类中使用这个方法;Super允许你请求父类的方法。这只在提高代码可读性时有用。

如前所述,在默认构造函数内部,在构造函数的第一行调用隐式super()。

这个super()自动调用从类层次结构顶部开始的构造函数链,并向下移动层次结构。

如果程序的类层次结构中有两个以上的类,顶层类的默认构造函数将首先被调用。

下面是一个例子:

class A {
    A() {
    System.out.println("Constructor A");
    }
}

class B extends A{

    public B() {
    System.out.println("Constructor B");
    }
}

class C extends B{ 

    public C() {
    System.out.println("Constructor C");
    }

    public static void main(String[] args) {
    C c1 = new C();
    }
} 

以上将输出:

Constructor A
Constructor B
Constructor C