java - 为什么子类不能访问 Kotlin 中的嵌套接口?
问题描述
在 Java 中,我可以定义一个嵌套在内部的接口MainClass
,并且我仍然可以通过 any 引用该接口SubClass
,如下所示:
public class MainClass {
interface MyInterface {
public void printName();
}
}
public class SubClass extends MainClass {
}
// Notice here that I use `SubClass.MyInterface` so the client doesn't know MainClass exists.
public void handleInterface(SubClass.MyInterface implementation) {
}
但是,如果我尝试在 Kotlin 中做同样的事情,它就不起作用:
open class MainClass {
interface MyInterface {
fun printName()
}
}
class SubClass : MainClass()
// This will not compile unless I do `MainClass.MyInterface`. See the Java notes about why I might want that.
fun handleInterface(implementation: SubClass.MyInterface) {
}
我已经有过我们不需要的辩论,SubClass.MyInterface
可以从定义它的基类中引用它,但我真的很好奇为什么 Kotlin 不支持这一点,而 Java 似乎允许它。
解决方案
推荐阅读
- bash - 使用 bash 将十六进制转换为十进制
- curl - 在 Arduino 中使用字节缓冲区调用 curl 命令
- azure - 如何获取在特定时间有多少消息失败 iot hub
- html - CSS 文件未在 HTML 上加载
- angular - 在 Angular 的反应形式中添加值 FromArray
- javascript - nativescript localize 无法切换语言
- swift - 如何在 Swift 4+ 中一步解开可选字典值?
- cvxpy - 我正在尝试使用 cvxpy 最小化分段函数
- mongodb - 有条件地 $project 来自不同领域
- android - 无法在 Android Studio 中解析符号“MainThread”、Nullable 和“WorkerThread”