“无法直接访问抽象成员。” 我如何克服继承接口的这一方面?

huangapple go评论66阅读模式
英文:

"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&#39;?
[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&#39;t access abstract member directly` (why?).

And not just &#39;why&#39;, 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>



huangapple
  • 本文由 发表于 2023年4月20日 07:34:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059532.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定