构造函数调用必须是具有继承的构造函数中的第一条语句
Constructor call must be the first statement in a constructor with inheritance
我有我的父抽象 JUnitTest class:
public abstract class RestWSTest
{
public RestWSTest()
{
}
@Before
public void setUp() throws Exception
{
...
}
@After
public void tearDown() throws Exception
{
...
}
}
然后我想要一个扩展 RestWSTest
的 class,像这样:
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() 调用。
相反,您可以使用语法 access/invoke 超级 class 方法: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()
{
...
}
}
有关详细信息,请参阅以下内容 link:
https://docs.oracle.com/javase/tutorial/java/IandI/super.html
我有我的父抽象 JUnitTest class:
public abstract class RestWSTest
{
public RestWSTest()
{
}
@Before
public void setUp() throws Exception
{
...
}
@After
public void tearDown() throws Exception
{
...
}
}
然后我想要一个扩展 RestWSTest
的 class,像这样:
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() 调用。
相反,您可以使用语法 access/invoke 超级 class 方法: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()
{
...
}
}
有关详细信息,请参阅以下内容 link: https://docs.oracle.com/javase/tutorial/java/IandI/super.html