如果在Eclipse/STS中输入以下内容(使用groovy):
interface iFaceWithAnIssue {
def thisIsFine(a,b,c)
def thisHasProblems(alpha='va')
}
Run Code Online (Sandbox Code Playgroud)
抱怨的唯一一行是尝试使用默认值的那一行.如果支持与否,我无法从codehaus网站告诉我.
IDE错误是:
Groovy:Cannot specify default value for method parameter
Run Code Online (Sandbox Code Playgroud)
所以这让我觉得它不受支持.由于会有多个实现,我想在这里使用一个接口.我真的不需要接口中的默认值,但是如果实现类然后尝试默认此参数,则尝试完成接口契约时会出错.有什么办法吗?
tim*_*tes 10
你不能.
当您定义默认值时,Groovy实际上会在您的类中创建多个方法,例如:
class Test {
void something( a=false ) {
println a
}
}
Run Code Online (Sandbox Code Playgroud)
实际创造
public void something(java.lang.Object a) {
this.println(a)
}
Run Code Online (Sandbox Code Playgroud)
和
public void something() {
this.something(((false) as java.lang.Object))
}
Run Code Online (Sandbox Code Playgroud)
这不能像接口那样完成.
你可以这样做:
interface iFaceWithAnIssue {
def thisHasProblems()
def thisHasProblems(alpha)
}
Run Code Online (Sandbox Code Playgroud)
然后
class Test implements iFaceWithAnIssue {
// This covers both Inteface methods
def thisHasProblems(alpha='va') {
// do something
}
}
Run Code Online (Sandbox Code Playgroud)