Kotlin`这个`不会在inheritance类中返回正确的实例
我有两个class。 一个是另一个的父母。 我正在实例化Foo中的HashMap的obect,并尝试访问该对象,但在类Bar中,我得到的presenter as Map
不presenter as HashMap
presenter as Map
的引用,因此我的HashMap方法调用都没有工作。
我已经阅读了文档,并试图在init{...}
里面指定this.presenter
,就像在Java中一样,但是我仍然无法从子类的内部访问HashMap 。
open class Foo { var presenter = Map init { presenter = HashMap } } open class Bar : Foo() { //this is trying to call .put on the Map interface, so I get an error presenter.put(someData) }
您必须将types指定为MutableMap
,还要指定Map的types:
open class Foo { val presenter: MutableMap = HashMap() } open class Bar : Foo() { //this is trying to call .put on the Map interface, so I get an error fun doit(){ presenter.put("","") } }
你让编译器推断你的presenter
的types,这是一个只读的地图,没有put
。
- Kotlin代码编译为Jar以用于Java Project?
- 如何在Kotlin Native中导入Java包
- 私人获得者和Kotlin财产的公共二传手
- 找到重复的蓝牙设备。 我在这里双倍浸泡? 也能够更新recyclerview
- Kotlin对象和伴侣对象如何与懒惰一起工作的内存
- 如何阅读Chronicle队列中的最后N个记录?
- Spring Boot:添加@Transactional会产生java.lang.ClassNotFoundException:org.aspectj.util.PartialOrder $ PartialComparable
- Kotlin反射问题
- 不能从最终的类错误inheritance