今天面试碰到一道题,是关于Constructor call must be the first statement in a constructor的问题的。
题目是这样的
public class TestA {
public TestA(){
System.out.println("A");
}
}
public class TestB extends TestA {
protected TestB(Object a){
System.out.println("B");
}
public TestB(){
super();
this.TestB(new Object());
}
}
public class Test {
public static void main(String[] ars)
{
new TestB();
}
}
super()和this.TestB(new Object());不能共存,否则编译时会报异常,换句话说就是super()和this.TestB(new Object());都必须在方法(构造方法)的第一行。
因此要成功编译上面的代码,必须要把TestB(Object a)这个方法修改成非构造方法,比如在前面加上void。
要了解更多请参考http://java.syntaxerrors.info/index.php?title=Constructor_call_must_be_the_first_statement_in_a_constructor
分享到:
相关推荐
The definition of an inline function needs to be in a header file, so that the compiler has the definition available for inlining at the call sites. However, implementation code properly belongs in ....
The first array will reflect to a row and the second one is column. int array Array is a collection of same data type. Suppose if we have declared an array of type int then the array will take only...
Returns True if the string must be enclosed in quotes (flex-property value) before saving in the filer. - ADD: In the class TFlexGrid added the methods DoSnap and DoCustomSnap (the last one can be ...
Watchface-Constructor Please check [our article] (https://yalantis.com/blog/building-a-customizable-constructor-for-designers-of-android-wear-watch-face-apps) This is simple watchface constructor ...
With the JavaMail API, in order to communicate with a server, you need a provider for a protocol. The creation of protocol-specific providers is not covered in this course because Sun provides a ...
Causes the next filter in the chain to be invoked, or if the calling filter is the last filter in the chain, causes the resource at the end of the chain to be invoked. doFilter(ServletRequest, ...
本文档时关于Java的反射的一个组件Constructor的描述和讲解
The program considers a word to be misspelled if the word does not exist in the hash table. When this occurs, the program displays the line number the word appeared in, the word, and a list of ...
BOA Constructor是Python 下用GTK进行图形编程的软件,界面很象Delphi. Getting Started Guide for boa Constructor boa constructor 教程
The first way is to call a global function through a function pointer that is a private member of your class. The client has to set that function pointer if it wants to handle that event. So, your ...
问题回顾:在编写完智能合约之后,通过命令行的形式编译智能合约时出现的问题。 1. 具体合约代码(错误写法): pragma solidity >=0.4.21 <0.7.0; contract Simple { string name; uint age; //定义事件 ...
The program considers a word to be misspelled if the word does not exist in the hash table. When this occurs, the program displays the line number the word appeared in, the word, and a list of ...
实例 c# constructor
* Show the information in a dialog which has the specified title and belongs to the * specified component. * * @param parent The component cause the exception. * @param title The title of ...
Boa-constructor 是一个很好的wxpython gui设计软件
In addition, the statically linked CyAPI.lib file must be linked to your project. Versions of the .lib file are available for use with Microsoft Visual C++ 6 and 7, and Borland C++ Builder 6.0. The ...
Write a constructor that takes a parameter for each of the attributes listed above and sets them within the objects by calling the setter methods listed below. The Person class should have a setter ...
附件是自己总结在编程过程中所遇到的关于反射的内容,主要是详细的描述了关于Constructor、Mehod、Field和Class类的内容
分析javascript中 prototype __proto__ constructor之间的关系
boa-constructor-0.6.1. for python2.5 支持拖拽的ui IDE