【问题标题】:super() in JavaJava中的super()
【发布时间】:2011-04-15 14:32:49
【问题描述】:

super()是用来调用父构造函数的吗? 请解释super()

【问题讨论】:

标签: java super


【解决方案1】:

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

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

它也可以用来调用父级的方法。 IE。 super.aMethod()

更多信息和教程here

【讨论】:

  • 注意:在第一种情况下,父类必须有一个无参数构造函数,失败会抛出编译错误。
  • Nit:父/子术语对于类层次结构不是很好。 (孩子不是父亲。)
  • super的使用位置有限制吗?
  • @VivekChavda,当然,孩子可以做父母,就像学生可以做老师等等。但我想你明白与 Animal -> Dog 相比的区别例如。狗必然是动物。父母/孩子通常是一个有一个关系(“父母有一个孩子”),而动物/狗是一个“是一个”关系。见aioo.be/2016/06/29/a-child-is-not-always-a-parent.html
  • @AaronFranke, "在哪里可以使用 super 有任何限制吗?" -- 是的,super(...) 只能用作构造函数中的第一条语句。
【解决方案2】:

一些事实:

  1. super() 用于调用直接父级。
  2. super() 可用于实例成员,即实例变量和实例方法。
  3. 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 中,我们在打印其输出时使用了super() 和变量a,而不是打印派生类的变量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()函数。

【讨论】:

  • 你为什么不缩进你的源代码示例?有什么具体原因吗?
  • NO er​​ikb,我想知道super()的用法,以后只有我去
  • 在我的基类中,我用一、二、...参数重载构造函数
  • 但在我的派生类中,我使用 super() 没有任何参数。那么是否自动调用基类的默认构造函数会发生什么
  • super() 不是关键字。这是一个构造函数调用。 super 是一个关键字,#1 和 #2 仅在该定义下才有意义。
【解决方案3】:

来源文章:Java: Calling 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() 而不使用任何参数。那么是否自动调用基类的默认构造函数会发生什么
  • 是的。如果您调用super(),它将调用不带参数的超类的构造函数。同样,如果您执行super(arg1),它将调用 1-argument 构造函数,依此类推。
  • 如果基类中没有没有任何参数的构造函数,那么如果派生类调用 super() 会发生什么。
  • 没什么。它不会编译。如果您自己提供构造函数,则不会生成自动/默认/无参数构造函数,因此super() 将不是有效调用。
【解决方案4】:

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

是的。

请解释一下 Super()。

super()super 关键字的特殊用法,您可以在其中调用无参数的父构造函数。一般来说,super 关键字可用于调用被覆盖的方法、访问隐藏字段或调用超类的构造函数。

这是official tutorial

【讨论】:

  • super() 用于调用父构造函数,super.myMethod() 用于调用被覆盖的方法。
  • @seanizer 或超类的任何其他方法(包括静态方法)(如果在范围内)。 super 只是对您的基类的引用。
  • 我不认为 super() 用于调用基类方法。不过你可以使用 super.method()。
【解决方案5】:

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

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

class Implicit {
    Implicit() {
    }
}

【讨论】:

    【解决方案6】:

    是的,super()(小写)调用父类的构造函数。您可以包含参数:super(foo, bar)

    还有一个super关键字,你可以在方法中使用它来调用超类的方法

    “Java super”的快速谷歌搜索结果为this

    【讨论】:

      【解决方案7】:

      没错。 Super 用于调用父构造函数。所以假设你有一个这样的代码块

      class A{
          int n;
          public A(int x){
              n = x;
          }
      }
      
      class B extends A{
          int m;
          public B(int x, int y){
              super(x);
              m = y;
          }
      }
      

      然后就可以给成员变量n赋值了。

      【讨论】:

        【解决方案8】:

        我已经看到了所有的答案。但是大家都忘了提一个很重要的点:

        super() 应该在构造函数的第一行调用或使用。

        【讨论】:

          【解决方案9】:

          只是 super();单独将调用默认构造函数,如果它存在于类的超类中。但是您必须自己显式编写默认构造函数。如果你没有,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.
             }
          
          }
          

          【讨论】:

          • 没有参数
          【解决方案10】:

          例如,在 selenium 自动化中,您有一个 PageObject 可以像这样使用其父级的构造函数:

          public class DeveloperSteps extends ScenarioSteps {
          
          public DeveloperSteps(Pages pages) {
              super(pages);
          }........
          

          【讨论】:

            【解决方案11】:

            我想与代码分享我理解的任何内容。

            java中的super关键字是一个引用变量,用来引用父类对象。它主要用于以下情况:-

            1.使用 super 和变量:

            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
            
            1. 使用 super with 方法:
            /* 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
            

            3.在构造函数中使用 super:

            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
            

            【讨论】:

              【解决方案12】:

              我们可以用 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();
                  }
              }
              

              在创建对象时,JVM总是先执行类中的构造函数 在继承树的顶层。然后一直向下继承树。 可能发生这种情况的原因是 Java 编译器会自动插入一个调用 到超类的无参数构造函数。如果没有无参数构造函数 在超类和子类中没有明确说明哪个构造函数是 在超类中执行,你会得到一个编译时错误。

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

              【讨论】:

                【解决方案13】:

                构造函数
                在构造函数中,您可以使用它不带点来调用另一个构造函数。 super 调用超类中的构造函数; this 调用此类中的构造函数:

                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 在超类需要初始化自身时很有用。 this 非常有用,它允许您在一个构造函数中只编写一次所有硬初始化代码,并从所有其他更容易编写的构造函数中调用它。

                方法
                在任何方法中,您都可以将它与点一起使用来调用另一个方法。 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 允许您改为请求超类的方法。 this 仅用作使您的代码更具可读性的一种方式。

                【讨论】:

                  【解决方案14】:

                  super关键字可用于调用超类的构造函数并引用超类的成员

                  当您使用正确的参数调用 super() 时,我们实际上调用了构造函数 Box,它初始化变量 widthheightdepth,通过使用值来引用它的相应参数。你只剩下初始化它的增值权重了。如有必要,您现在可以将 Box 类变量设置为 private。将 Box 类私有修饰符的字段放入并确保您可以毫无问题地访问它们。

                  在超类可以有多个重载版本的构造函数,所以你可以用不同的参数调用方法super()。程序将执行与指定参数匹配的构造函数。

                  public class Box {
                  
                      int width;
                      int height;
                      int depth;
                  
                      Box(int w, int h, int d) {
                          width = w;
                          height = h;
                          depth = d;
                      }
                  
                      public static void main(String[] args){
                          HeavyBox heavy = new HeavyBox(12, 32, 23, 13);
                      }
                  
                  }
                  
                  class HeavyBox extends Box {
                  
                      int weight;
                  
                      HeavyBox(int w, int h, int d, int m) {
                  
                          //call the superclass constructor
                          super(w, h, d);
                          weight = m;
                      }
                  
                  }
                  

                  【讨论】:

                    【解决方案15】:

                    super 是一个关键字。它在子类方法定义中用于调用超类中定义的方法。不能调用超类的私有方法。 super 关键字只能调用公共和受保护的方法。类构造函数也使用它来调用其父类的构造函数。

                    查看here 以获得进一步解释。

                    【讨论】:

                      【解决方案16】:

                      如上所述,在默认构造函数中,在构造函数的第一行调用了一个隐式 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
                      

                      【讨论】:

                        【解决方案17】:
                        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() 可用于调用直接父类构造函数。

                        【讨论】:

                        • 这个答案没有添加任何未提供的by this answer 是在您之前 10 年发布的。
                        猜你喜欢
                        • 2023-03-10
                        • 2016-08-01
                        • 1970-01-01
                        • 1970-01-01
                        • 2013-09-01
                        • 2014-09-07
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        相关资源
                        最近更新 更多