Constructor call must be the first statement in a constructor
代码样例
- /**
- * Super的错误类型
- *
- * @author 赵学庆,Java世纪网(java2000.net)
- *
- */
- class T {
- T() {
- int i = 0 ;
- super (i);
- }
- T( int id) {
- super ();
- super ();
- }
- }
错误解释:
在构造函数中调用父类构造函数super/this(...)必须位于第一句
解决方案:
将语句super/this位于第一句,且只能调用一次,因为第二个就不是第一行了。
- class T {
- T() {
- this ( 0 );
- }
- T( int id) {
- super ();
- }
- }
构造函数调用
本文介绍了在Java中构造函数调用父类构造函数super/this(...)的规则,即必须放在构造函数的第一条语句,并且只能调用一次。通过示例展示了正确的调用方式。
897

被折叠的 条评论
为什么被折叠?



