Java: Interface with Subclass
我正在使用带子类的接口。 我收到了一些错误:
Syntax error on token"{", { expected after this token
Multiple markers at this line
- Method breakpoint:TestSubClass [entry] -
doSomething(String)
- Syntax error on token")", ; expected
- Syntax error on token"(", ; expected
- implements TestInterface.doSomething
Syntax error, insert"}" to complete ClassBody
从我的代码我认为我已经实现了一切正常。 这是一个额外的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | public interface TestInterface { public void doSomething (String text); } public class TestBaseClass { private String name; public TestBaseClass() { } public TestBaseClass(String name){ } public String getName() { return name; } public void setName(String name) { this.name = name; } } public class TestSubClass extends TestBaseClass implements TestInterface { super(name); public void doSomething (String text){ } } |
1 2 3 4 5 6 7 8 |
你不能只在类中调用
正如@Kayaman暗示的那样,您需要一个构造函数定义来委托超类的构造函数:
1 2 3 4 5 6 7 8 9 10 11 |
1 2 3 4 5 6 7 8 | public class TestSubClass extends TestBaseClass implements TestInterface{ //This should be called inside a constructor super(name); //Ok here public void doSomething (String text){} } |
这样做:
1 2 3 4 5 6 7 | public class TestSubClass extends TestBaseClass implements TestInterface{ public TestSubClass (){ super(name); } public void doSomething (String text){} } |