构造函数中是否有必要添加 super() ?

发布于 2024-08-17 07:14:26 字数 125 浏览 7 评论 0原文

如果我不将其放入子类的构造函数中,这不是由编译器自动放入的吗?

也就是说,我根本不需要关心它?他们在一些文章中提出了这一点。

如果我有一个带参数的构造函数,这将是构造函数,还是采用不带参数列表的构造函数?

Isn't this one automatically put by the compiler if I don't put it in a subclass's constructor?

That means I don't even need to care about it? In some articles they put it out.

And if I've got one constructor with arguments, will this be the constructor, or does it take a constructor without argument list?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

世界等同你 2024-08-24 07:14:27

首先是一些术语:

  • 无参数构造函数:没有参数的构造函数;
  • 可访问的无参数构造函数:超类中对子类可见的无参数构造函数。这意味着它要么是公共的,要么是受保护的,或者,如果两个类位于同一个包中,则为包访问;和
  • 默认构造函数:当类中没有显式构造函数时,编译器添加的公共无参数构造函数。

所以所有类都至少有一个构造函数。

子类构造函数可能首先指定在执行子类构造函数中的代码之前调用超类中的哪个构造函数。

如果子类构造函数没有指定要调用哪个超类构造函数,则编译器将自动调用超类中可访问的无参数构造函数。

如果超类没有无参数构造函数或不可访问那么不指定要调用的超类构造函数(在子类构造函数中)会出现编译器错误,因此必须指定它。

例如:

public class Base { }
public class Derived extends Base { }

这很好,因为如果您没有显式添加构造函数,Java 会为您添加一个公共默认构造函数。

public class Base { }
public class Derived extends Base { public Derived(int i) { } }

也还好。

public class Base { public Base(String s) { } }
public class Derived extends Base { }

上面是一个编译错误,因为 Base 没有默认构造函数。

public class Base { private Base() { } }
public class Derived extends Base { }

这也是一个错误,因为 Base 的无参数构造函数是私有的。

Firstly some terminology:

  • No-args constructor: a constructor with no parameters;
  • Accessible no-args constructor: a no-args constructor in the superclass visible to the subclass. That means it is either public or protected or, if both classes are in the same package, package access; and
  • Default constructor: the public no-args constructor added by the compiler when there is no explicit constructor in the class.

So all classes have at least one constructor.

Subclasses constructors may specify as the first thing they do which constructor in the superclass to invoke before executing the code in the subclass's constructor.

If the subclass constructor does not specify which superclass constructor to invoke then the compiler will automatically call the accessible no-args constructor in the superclass.

If the superclass has no no-arg constructor or it isn't accessible then not specifying the superclass constructor to be called (in the subclass constructor) is a compiler error so it must be specified.

For example:

public class Base { }
public class Derived extends Base { }

This is fine because if you add no constructor explicitly Java puts in a public default constructor for you.

public class Base { }
public class Derived extends Base { public Derived(int i) { } }

Also fine.

public class Base { public Base(String s) { } }
public class Derived extends Base { }

The above is a compilation error as Base has no default constructor.

public class Base { private Base() { } }
public class Derived extends Base { }

This is also an error because Base's no-args constructor is private.

寄与心 2024-08-24 07:14:27

如果超类构造函数没有参数,Java 会自动为您调用它。如果它有参数,你会得到一个错误。

src: http://java.sun.com/docs/books /tutorial/java/IandI/super.html

If the super class constructor has no arguments Java automatically calls it for you. If it has arguments you'll get an error.

src: http://java.sun.com/docs/books/tutorial/java/IandI/super.html

七度光 2024-08-24 07:14:27

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

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

class Implicit {
    Implicit() {
    }
}

更新(2018 年 12 月):

编写显式 super() 有助于在 IDE 中导航源代码。

截至 2018 年 12 月,Eclipse 和 IntelliJ 均未提供任何从构造函数轻松导航的方法派生类到基类的构造函数。

Calling the no-arguments super constructor is just a waste of screen space and programmer time. The compiler generates exactly the same code, whether you write it or not.

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

class Implicit {
    Implicit() {
    }
}

Update (December 2018):

Writing an explicit super() helps navigating the source code in the IDE.

As of December 2018, neither Eclipse nor IntelliJ provide any means of comfortably navigating from the constructor of the derived class to the constructor of the base class.

冬天的雪花 2024-08-24 07:14:27

即使您不调用默认父构造函数,也会从子默认构造函数调用它。

主要

public class Main {

    public static void main(String[] args) {
        A a = new B();
    }
}

A

public class A {

    public A() {
        System.out.println("A");
    }
}

B

public class B extends A {

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

印刷品

A
B

Default parent constructor is called from child default constructor even if you do not call it.

Main

public class Main {

    public static void main(String[] args) {
        A a = new B();
    }
}

A

public class A {

    public A() {
        System.out.println("A");
    }
}

B

public class B extends A {

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

Prints

A
B
故人爱我别走 2024-08-24 07:14:27

如果没有显式调用 super([arguments]),任何类构造函数总是调用“super()”,只有我们在编程时记住对超类构造函数的访问......
当我们不扩展任何特定类时,自动扩展 java.lang.Object 类

Any class constructor always calls "super()" if there is not explicitly called super([arguments]), only we keep in mind access of super class constructor while programming...
when we not extends any specific class automatic extends java.lang.Object class

梦醒时光 2024-08-24 07:14:27

让我们记住 super 关键字..
它只是调用超类构造函数来创建对象。

情况1:当存在超类时,

class Base {}
class Derived{}

super关键字会隐式添加到派生类中。

class Base {}
class Derived extends Base {super();}

所以上面的代码与

情况2:(当有超类时)相同,但构造函数是重载

class Base{
   Base(int x) {
        System.out.println("Base");
    }
}
class Derived extends Base {
    Derived() {
        super(); // here super will give ERROR as there is no default constructor present because we overloaded the constructor
        super(2); // will work, but without this it will not work as we have to call constructor of super class
    }
}

情况3:(当没有您定义的超类时)。

众所周知,每个类都是 Object 类的子类。或者我们可以说基类是所有类的超类。
所以现在添加或不添加 super 没有任何意义.. jvm 将默认添加它。

lets remember super keyword..
its nothing but calling the superclass constructor for creating the object.

case 1: when there is super class

class Base {}
class Derived{}

here super keyword is implicitly added in derived class.

class Base {}
class Derived extends Base {super();}

so the above code is same as this

case 2: when there is super class but constructor is overload

class Base{
   Base(int x) {
        System.out.println("Base");
    }
}
class Derived extends Base {
    Derived() {
        super(); // here super will give ERROR as there is no default constructor present because we overloaded the constructor
        super(2); // will work, but without this it will not work as we have to call constructor of super class
    }
}

case 3: when there is no super class defined by you.

As we know every class is subclass of Object class.. or we can say base class is super class of all the classes.
so now add super or not does not make any sense.. by jvm it will be added by default.

允世 2024-08-24 07:14:27
abstract class Book
 {
String title;
String author;
Book(String t,String a){
    title=t;
    author=a;
}
abstract void display();

}    

如果超类可以有一个无参数构造函数。最好有一个无参数构造函数,否则你必须传递带参数的超级构造函数。

如果超类没有无参数构造函数或者不可访问,则不指定要调用的超类构造函数(在子类构造函数中)会出现编译器错误,因此必须指定它

class MyBook extends Book{   
int price ;
public  MyBook(String t,String a,int price){
     super(t,a);
    this.price=price;
}

public void display(){
    System.out.println("Title: "+title);
 System.out.println("Author: "+author); 
System.out.println("Price: "+price);

}

}

abstract class Book
 {
String title;
String author;
Book(String t,String a){
    title=t;
    author=a;
}
abstract void display();

}    

If super class can have a No-args constructor .It is good to have a no argument constructor otherwise you have to pass super constructor with parameter .

If the superclass has no no-arg constructor or it isn't accessible then not specifying the superclass constructor to be called (in the subclass constructor) is a compiler error so it must be specified

class MyBook extends Book{   
int price ;
public  MyBook(String t,String a,int price){
     super(t,a);
    this.price=price;
}

public void display(){
    System.out.println("Title: "+title);
 System.out.println("Author: "+author); 
System.out.println("Price: "+price);

}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文