2024-12-14 08:00:05

Java中的super()

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


当前回答

调用无参数的超级构造函数只是浪费屏幕空间和程序员时间。编译器生成的代码完全相同,不管你写不写。

class Explicit() {
    Explicit() {
        super();
    }
}

class Implicit {
    Implicit() {
    }
}

其他回答

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

Java超级关键字的使用

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

只是超级();Alone将调用默认构造函数,如果类的超类中存在默认构造函数。但是必须自己显式地编写默认构造函数。如果你不这样做,Java会为你生成一个没有实现的,保存super();,引用通用超类对象,并且不能在子类中调用它。

public class Alien{
   public Alien(){ //Default constructor is written out by user
   /** Implementation not shown…**/
   }
}

public class WeirdAlien extends Alien{
   public WeirdAlien(){
   super(); //calls the default constructor in Alien.
   }

}

构造函数 在构造函数中,可以使用它而不带点来调用另一个构造函数。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来调用被覆盖的方法,比如super. methodname ();

调用超类构造函数

如果构造函数没有显式调用超类构造函数,Java编译器会自动插入对超类的无参数构造函数的调用。如果超类没有无参数构造函数,则会得到编译时错误。

看看下面的代码:


class Creature {
    public Creature() {
        system.out.println("Creature non argument constructor.");
    }
}

class Animal extends Creature {
    public Animal (String name) {
        System.out.println("Animal one argument constructor");
    }
    public Animal (Stirng name,int age) {
        this(name);
        system.out.println("Animal two arguments constructor");
    }
}

class Wolf extends Animal {
    public Wolf() {
        super("tigerwang",33);
        system.out.println("Wolf non argument constructor");
    }
    public static void main(string[] args) {
        new Wolf();
    }
}

When creating an object,the JVM always first execute the constructor in the class of the top layer in the inheritance tree.And then all the way down the inheritance tree.The reason why this is possible to happen is that the Java compiler automatically inserts a call to the no-argument constructor of the superclass.If there's no non-argument constructor in the superclass and the subclass doesn't explicitly say which of the constructor is to be executed in the superclass,you'll get a compile-time error.

类的构造函数,在上面的代码中,如果我们想成功地创建Wolf对象 类必须执行。在这个过程中,Animal中的双参数构造函数 类被调用。同时,它显式地调用同一个参数中的单参数构造函数 类和单参数构造函数隐式调用生物中的非参数构造函数 类和非参数构造函数再次隐式调用Object中的空构造函数 类。

super()是用来调用父构造函数吗?

是的。

请解释一下超级()。

Super()是Super关键字的一种特殊用法,用于调用无参数的父构造函数。通常,super关键字可用于调用被重写的方法、访问隐藏字段或调用超类的构造函数。

下面是官方教程