任何方式_not_调用超类构造函数在Java? [英] Any way to _not_ call superclass constructor in Java?

查看:146
本文介绍了任何方式_not_调用超类构造函数在Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个类:

class A {
   public A() { }
}

和另一个

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

有任何方式获得 BB() 不是来调用 AA()

is there any way to get B.B() not to call A.A()?

推荐答案

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

There is absolutely no way to do this in Java; it would break the language specification.

JLS 12执行/ 12.5创建新类实例


在作为结果返回对新创建的对象的引用之前,使用以下过程处理指示的构造函数以初始化新对象:

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:


    <如果这个构造函数从显式构造函数调用开始,在同一个类中使用另一个构造函数(使用 this ),然后[...]
  1. 这个构造函数不是从同一个类中另一个构造函数的显式构造函数调用开始的(使用 this )。 如果这个构造函数用于 Object 以外的类,那么这个构造函数将以超类构造函数的显式或隐式调用开始( super )。

  2. 为此类执行实例初始化器和实例变量初始化器[...] [...]

  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 [...]


这篇关于任何方式_not_调用超类构造函数在Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆