本文介绍了我想在多级继承中仅调用子类构造函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
构造器.如果只想打印构造函数C",则需要执行以下任一操作:So a super constructor is always called. If you want to print only "Constructor C", then you need to do any of the following:
- 更改B的构造函数,以使其不再通过删除println语句或一起删除无参数的构造函数来打印构造函数B".
-
在B中添加第二个构造函数,该构造函数不打印任何内容,并从C调用它:
- Change the constructor of B so it no longer prints "Constructor B" by either removing the println statement or removing the no-argument constructor alltogether.
Add a second constructor within B which does not print anything and call it from C:
B(boolean b) { }
C() { super(true); }
确保C不再扩展B:
Make sure C does not extend B anymore:
class C { public C() { System.out.println("Constructor C"); } }
这篇关于我想在多级继承中仅调用子类构造函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!