简体   繁体   English

访问Kotlin中的父Java类的(静态类)成员

[英]access (static class) member of parent java class in kotlin

I've got a java class A that has a static class as a member: 我有一个具有静态类作为成员的Java类A

class A {
  ...
  static class B {...}
}

I see code in java that just do AB to access class B. However, I cannot do the same thing in my kotlin class: 我在Java中看到仅执行AB来访问类B的代码。但是,在我的kotlin类中我不能做同样的事情:

import org.mypackage.A
class C {
  ...
  fun doSomething(o: Any) {
    if (o is A.B) { ...}
    ...
  }
}

(intellij marks B as red when I do AB . (当我执行AB时,intellij将B标记为红色。
My use case: I have an object that I want to cast to B , like o is AB . 我的用例:我有一个要投射到B的对象,例如o is AB

I did mvn clean compile and got this error: Cannot access 'B': it is public/*package*/ in 'A' . 我没有执行mvn clean compile并收到以下错误: Cannot access 'B': it is public/*package*/ in 'A'

My question: how do I access B in my kotlin class? 我的问题:我如何在Kotlin课堂上访问B

If C has to import class A then it's not in the same package as A and B. If that's the case, how should C see class B which is not public but package level visible. 如果C必须导入类A,则它与A和B不在同一个包中。如果是这样,C应该如何看待不是公共的但包级别可见的类B。 It does not work in Java as well. 它在Java中也不起作用。 (In Kotlin there is no package level visibility modifier, one would probably take "internal" but that's not the same. Those would see eachother) (在Kotlin中,没有包装级别的可见性修改器,可能会带“内部”,但这并不相同。那些会互相看到)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM