![](/img/trans.png)
[英]Constructor call must be the first statement in a constructor in super()
[英]Constructor call must be the first statement in a constructor with inheritance
我有我的父抽象JUnitTest類:
public abstract class RestWSTest
{
public RestWSTest()
{
}
@Before
public void setUp() throws Exception
{
...
}
@After
public void tearDown() throws Exception
{
...
}
}
然后,我想擁有一個擴展RestWSTest
的類,如下所示:
public class RestWSCreateGroupTest extends RestWSTest
{
public RestWSCreateGroupTest()
{
super();
}
@Before
public void setUp() throws Exception
{
super(); --> *Constructor call must be the first statement in a constructor*
...
}
@After
public void tearDown() throws Exception
{
super(); --> *Constructor call must be the first statement in a constructor*
...
}
@Test
public void testCreateGroup()
{
...
}
}
為什么會收到錯誤消息? 我有一個構造函數,在那里我調用了super()
,所以我真的不知道該怎么辦...
方法public void setUp()
不是構造函數。 您不能調用super();
在里面。 我想你打算用super.setUp();
您不能在構造函數方法之外使用super()調用。
換句話說,setUp()和tearDown()是方法,它們不是構造函數,因此您不能使用super()調用。
相反,您可以使用以下語法訪問/調用超類方法:super.mySuperClassMethod();
因此,如下更改代碼:
public class RestWSCreateGroupTest extends RestWSTest
{
public RestWSCreateGroupTest()
{
super();
}
@Before
public void setUp() throws Exception
{
super.setUp();
...
}
@After
public void tearDown() throws Exception
{
super.tearDown();
...
}
@Test
public void testCreateGroup()
{
...
}
}
有關更多詳細信息,請參見以下鏈接: https : //docs.oracle.com/javase/tutorial/java/IandI/super.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.