英文:
"Can't access abstract member directly." How do I overcome this aspect of inheriting an interface?
问题
class meow() : InputConnection {
override fun getTextBeforeCursor(before: Int, after: Int): CharSequence? {
return super.getTextBeforeCursor(before, after)
}
}
但为什么继承接口既需要重写,又说“无法直接访问此内容”?
上述错误的原因
这张图片显示了错误。一个是因为需要重写更多内容(是的),另一个是为什么这个重写“无法直接访问抽象成员”(为什么?)。
而且不仅是“为什么”,还要如何正确继承具有这个特性的接口?
<details>
<summary>英文:</summary>
class meow(): InputConnection{
override fun getTextBeforeCursor(before:Int,after:Int):CharSequence?{
return super.getTextBeforeCursor(before,after)
}
}
but whai does inheriting an interface both require overriding, and say `cannot access this directly'?
[what the above errors][1]
This image is the errors from this. One for there being more stuff in need of override (yes) and another how this override `can't access abstract member directly` (why?).
And not just 'why', but hao inherit an interface with this aspect correctly?
[1]: https://i.stack.imgur.com/Lb0zc.png
</details>
# 答案1
**得分**: 0
你不能调用*抽象*函数,因为它没有定义在被调用时要执行什么操作。
如果一个接口声明一个没有默认实现的函数,那么它就是*抽象*的。
在这种情况下,显然InputConnection类没有为这个函数定义默认实现。`InputConnection.getTextBeforeCursor()`是抽象的,因此你不能使用`super.getTextBeforeCursor()`来调用它。
要正确执行此操作,你需要自己定义这个函数的内容以及在没有依赖于`super`版本的情况下它的行为。
<details>
<summary>英文:</summary>
You cannot call an *abstract* function because it has no definition of what to do when called.
If an interface declares a function without any default implementation, then it is *abstract*.
In this case, evidently the InputConnection class does not define a default implementation for this function. `InputConnection.getTextBeforeCursor()` is abstract, so you cannot call it using `super.getTextBeforeCursor()`.
To do this correctly, it is up to you to define the content of this function and how it will behave without any reliance on a `super` version of this function.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论