今天面试碰到一道题,是关于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 ...
CreateInstanceFromType is not allowed to be called from a ScriptableObject constructor (or instance field initializer), call it in OnEnable instead. Called from ScriptableObject '...
Java 反射机制中的 Constructor 类提供了关于类的单个构造方法的信息,包括对它的访问权限、Class 获取构造函数的方式等。下面将详细讲解 Constructor 类的概述、获取构造函数的方式、构造类的实例概述等。 ...
Constructor 属性可以帮助我们解决 typeof 函数无法解决的问题,即无法确定变量的具体类型。typeof 函数只能告诉我们变量是对象、数组或函数,但是无法告诉我们变量的具体类型,这时 constructor 属性就可以发挥作用...
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, ...
boa-constructor是一款基于Python语言,使用wxPython库构建图形用户界面(GUI)的应用程序开发工具。它为非专业程序员提供了一种直观、易用的可视化界面来设计和编写GUI程序,降低了开发复杂度,尤其适合初学者入门...
4. **调试工具**:Boa Constructor具备调试功能,可以设置断点,单步执行代码,查看变量值,帮助开发者定位和解决问题,从而提高开发效率。 5. **跨平台**:作为Python的工具,Boa Constructor支持多种操作系统,如...
在Solidity编程中,我们经常会遇到“TypeError: Data location must be “memory” for parameter in function, but none was given.”这样的错误。这个问题出现在你试图在函数参数中使用一个没有指定存储位置的数据...
constructor cannot be virtual(解决方案).md
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 experiment can be extended to include more features, such as adding more graphical classes, implementing more functions for the classes, and using the classes in a real-world application. ...
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 ...
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 ...
然而,这种方法在处理类继承时可能会出现问题。当一个子类继承自父类时,子类实例的`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 ...