gpt4 book ai didi

java - 在Java中_not_调用父类(super class)构造函数的任何方法?

转载 作者:IT老高 更新时间:2023-10-28 20:25:36 26 4
gpt4 key购买 nike

如果我有课:

class A {
public A() { }
}

还有一个

class B extends A {
public B() { }
}

有没有办法让B.B() 调用A.A()

最佳答案

在 Java 中绝对没有办法做到这一点;它会破坏语言规范。

JLS 12 Execution / 12.5 Creation of New Class Instances

Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:

  1. Assign the arguments for the constructor [...]
  2. If this constructor begins with an explicit constructor invocation of another constructor in the same class (using this), then [...]
  3. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super).
  4. Execute the instance initializers and instance variable initializers for this class [...]
  5. Execute the rest of the body of this constructor [...]

关于java - 在Java中_not_调用父类(super class)构造函数的任何方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2967662/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com