java中的构造函数

发布于 2024-12-02 22:40:50 字数 566 浏览 1 评论 0原文

public class A {
public A() {
    System.out.println("a1");
}
public A(int x) {
    System.out.println("a2");

}}

public class B extends A {
public B() {
    super(5);
    System.out.println("b1");
}
public B(int x) {
    this();
    System.out.println("b2");
}
public B(int x, int y) {
    super(x);
    System.out.println("b3");
}}

我不明白为什么当我运行 B b= new B();

B 扩展 A 时不应用 A 的默认构造,所以首先我们调用 A 的构造来打印“a1 ”,然后我们调用 A 的第二个结构,它打印“a2”,而 B() 打印“b1”,但是当我运行它时,它只打印“a2,b1”,所以显然A() 一开始就没有应用 - 为什么?

public class A {
public A() {
    System.out.println("a1");
}
public A(int x) {
    System.out.println("a2");

}}

public class B extends A {
public B() {
    super(5);
    System.out.println("b1");
}
public B(int x) {
    this();
    System.out.println("b2");
}
public B(int x, int y) {
    super(x);
    System.out.println("b3");
}}

I don't understand why the default constructure of A is not applied when I run B b= new B();

B extends A, so First we call the constrcture of A that supposed to print "a1", and then we call the the second constructure of A which prints "a2" and B() prints "b1", but when I run it, it prints only "a2,b1", so obviously A() wan't applied at the beginning- why?

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

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

发布评论

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

评论(4

梦与时光遇 2024-12-09 22:40:50

当您说 B b = new B(); 时,您正在调用默认构造函数,

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

因为它已经调用了其超级构造函数 [super(5)],所以编译器不会插入隐式默认构造函数。于是就有了结果。

注意:从您的问题来看,您似乎认为创建对象时会调用所有构造函数。恐怕这是不正确的。只有您显式调用来创建对象的构造函数才会被调用。如果该构造函数通过 this() 方法调用其他构造函数,则只有其他构造函数才会被调用。

When you say B b = new B(); you are calling the default constructor which is

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

Since this already has a call to its super constructor [super(5)] so the compiler will not insert the implicit default constructor. Hence the result.

NOTE: From your question, it seems that you have the idea that all the constructors are called when you create an object. I am afraid that is incorrect. Only that constructor will be called which you explicitly call to create the object. And if that constructor calls other constructor via the this() method, only then the other constructors will be called.

不离久伴 2024-12-09 22:40:50

B 扩展了 A,所以首先我们调用 A 的构造来打印“a1”

这个说法是不正确的。
在类 B 中,您的无参数构造函数

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

调用带有 int 参数的超类(类 A)的构造函数。

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

您永远不会调用 super(),因此当您调用 B 的任何构造函数时,不会调用打印“a1”的构造函数

。 调用超级构造函数必须是构造函数的第一行。如果你想调用超类的无参数构造函数(在本例中,是打印“a1”的构造函数),你可以写...

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

如果你没有指定调用超类构造函数,那么java将自动放入一个调用到无参数超级构造函数。

B extends A, so First we call the constrcture of A that supposed to print "a1"

This statement is incorrect.
In class B your no arguments constructor

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

calls the constructor of the superclass (class A) which takes an int parameter.

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

You never make a call to super() so the constructor that prints "a1" will not be called when you call any of B's constructors

Calling a super constructor must be the first line of a constructor. If you wish to call the no argument constructor of a superclass (in this case, the one that prints "a1"), you would write...

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

If you do not specify calling a super constructor, then java will automatically put in a call to the no argument super constructor.

守望孤独 2024-12-09 22:40:50

这是因为您在 B 构造函数中调用 super(5) ,它调用第二个 A 构造函数而不是第一个构造函数。

That's because you call super(5) in B constructor which call the second A constructor instead of the first one.

不醒的梦 2024-12-09 22:40:50

我认为你误解了 Java 如何处理构造函数。首先,默认情况下,Java 将为每个类仅调用一个构造函数,除非您使用 this(...) 明确告诉它调用更多构造函数。其次,被调用的一个构造函数是超类的默认构造函数(因此它将调用super(),而不是this() );所以类 A 实际上看起来像这样:

public class A {
  public A() {
     super(); // implicit call to super(), which is Object()
     System.out.println("a1");
  }
  public A(int x) {
    super(); // implicit call to super(), which is Object()
    System.out.println("a2");
  }
}

因此调用 A() 将隐式调用 Object(),但是调用 A(int x )隐式调用Object(),而不是 - 您似乎假设的 - A()

由于在 B 中您总是显式指定调用另一个构造函数,因此编译器不会添加任何内容。下面我添加了关于调用 super(...)this(...) 时会发生什么情况的注释,

   public class B extends A {
      public B() {
        super(5); // explicit call to A(5), no implict call to A()
        System.out.println("b1");
      }
      public B(int x) {
        this(); // explicit call to B(), no implicit call to A()
        System.out.println("b2");
      }
      public B(int x, int y) {
        super(x); // explict call to A(x), no implicit call to A()
        System.out.println("b3");
      }
    }

所以再次强调,要记住的重要一点是 Java将在任何构造函数的第一行插入对 super() 的调用,除非您使用 this(...)super(. ..)。它绝不会单独插入this()

I think you missunderstand how Java handles constructors. First of all, by default Java will call only one constructor per class, unless you explicitly tell it to call more using this(...). Secondly, that one construct that is called is the default constructor of the super class (so it will call super(), not this()); so class A actually looks like this:

public class A {
  public A() {
     super(); // implicit call to super(), which is Object()
     System.out.println("a1");
  }
  public A(int x) {
    super(); // implicit call to super(), which is Object()
    System.out.println("a2");
  }
}

So invoking A() will implicitly call Object(), however invoking A(int x) will also implicitly call Object(), and not - how you seem to assume - A().

Since in B you always explicitly specify to call another constructor, the compiler will not add anything. Below I added comments on what will happen on the calls to super(...) and this(...)

   public class B extends A {
      public B() {
        super(5); // explicit call to A(5), no implict call to A()
        System.out.println("b1");
      }
      public B(int x) {
        this(); // explicit call to B(), no implicit call to A()
        System.out.println("b2");
      }
      public B(int x, int y) {
        super(x); // explict call to A(x), no implicit call to A()
        System.out.println("b3");
      }
    }

So again, the important thing to remember is that Java will insert a call to super() in the first line of any constructor, unless you explicitly invoke another constructor using this(...) or super(...). It will never insert this() by itself.

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