【发布时间】:2016-03-10 10:50:03
【问题描述】:
我在 Groovy 中有这段代码,这是一个错误还是设计使然:
abstract class One {
String content
String description
String returnContent(){
return content
}
}
class Two extends One {
String getContent() {
content ?: description
}
}
Two two = new Two(description:"this is my description")
assert two.returnContent() == "this is my description"
我希望断言通过但实际上没有,所以调用 content 时似乎没有调用实例的 getContent() 方法。
【问题讨论】:
-
错误?不,这是你的代码。假设你是问题所在;第一,最后,永远。为什么会调用 getContent()?您的代码调用 returnContent()。这甚至可以编译吗?
-
这只是一个例子。我只是想知道为什么在访问内容字段(在 Groovy 中通常与调用 getter 相同)时,对于抽象类实际上并没有像这样工作。 @duffymo,我可以多次成为问题,但这次不是。
-
@FranGarcía 提供一个演示“问题”的示例
-
如果你运行我添加到帖子中的示例,它会失败,我认为它不应该。
标签: inheritance groovy abstract-class