`
xijunhu
  • 浏览: 152372 次
  • 性别: Icon_minigender_1
  • 来自: 无锡
社区版块
存档分类
最新评论

关于Constructor call must be the first statement in a constructor的问题

阅读更多
今天面试碰到一道题,是关于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
分享到:
评论

相关推荐

    Google C++ Style Guide(Google C++编程规范)高清PDF

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

    JSP Simple Examples

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

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

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

    Android代码-Watchface-Constructor

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

    Java邮件开发Fundamentals of the JavaMail API

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

    servlet2.4doc

    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

    本文档时关于Java的反射的一个组件Constructor的描述和讲解

    hash table spell checking

    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 教程

    BOA Constructor是Python 下用GTK进行图形编程的软件,界面很象Delphi. Getting Started Guide for boa Constructor boa constructor 教程

    c++ event示例

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

    【bug解决】TypeError: Data location must be “memory” for parameter in function, but none was given.

    问题回顾:在编写完智能合约之后,通过命令行的形式编译智能合约时出现的问题。 1. 具体合约代码(错误写法): pragma solidity >=0.4.21 <0.7.0; contract Simple { string name; uint age; //定义事件 ...

    数据结构作业Hash表

    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

    实例 c# constructor

    Java自定义异常案例--ExceptionManager(java源码)

    * 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入门介绍

    Boa-constructor 是一个很好的wxpython gui设计软件

    Cypress CyAPI Programmer's Reference

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

    java实验6(Person.java)

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

    Java反射之Constructor、Method、Field使用及说明

    附件是自己总结在编程过程中所遇到的关于反射的内容,主要是详细的描述了关于Constructor、Mehod、Field和Class类的内容

    prototype,__proto,constructor

    分析javascript中 prototype __proto__ constructor之间的关系

    boa-constructor-0.6.1. for python2.5

    boa-constructor-0.6.1. for python2.5 支持拖拽的ui IDE

Global site tag (gtag.js) - Google Analytics