简体   繁体   English

转换为Kotlin后如何访问Java接口静态变量

[英]How to access Java Interface Static variable after converted to Kotlin

I have a an Interface in Java with the static variable interfaceValue that I could access as below 我有一个带有静态变量interfaceValue Java interfaceValue ,可以通过以下方式访问

public class Experimenting {

    public interface MyInteface {
        int interfaceValue = 10;
    }

    class MyImpl implements MyInteface { }

    MyImpl myImpl = new MyImpl();

    void testing() {
        int getInterfaceValue = myImpl.interfaceValue;
    }
}

When I convert to Kotlin, it is as below 当我转换成Kotlin时,如下

class Experimenting {

    internal var myImpl = MyImpl()

    interface MyInteface {
        companion object {
            val interfaceValue = 10
        }
    }

    internal inner class MyImpl : MyInteface

    internal fun testing() {
        val getInterfaceValue = myImpl.interfaceValue
    }
}

However the myImpl.interfaceValue is showing compile error sign, where by it doesn't recognize the interfaceValue . 但是, myImpl.interfaceValue显示编译错误符号,此时它无法识别interfaceValue How could I still access my interfaceValue in Kotlin? 我如何仍可以在Kotlin中访问interfaceValue

The conversion converts the static final interfaceValue to a val in the companion Kotlins equivalent of that. 该转换将静态最终interfaceValue转换为与之companion Kotlins中的val Unfortunately the converter is not perfect and sometimes messes thing up. 不幸的是,转换器并不完美,有时会搞砸。 To access it 要访问它

import com.your.package.Experimenting.MyInteface.Companion.interfaceValue    
class Experimenting {

    internal var myImpl = MyImpl()

    interface MyInteface {
        companion object {
           const val interfaceValue = 10
        }
    }

    internal inner class MyImpl : MyInteface

    internal fun testing() {
        val getInterfaceValue = interfaceValue
    } 
}

does the trick. 绝招。

also does: 还可以:

class Experimenting {

    internal var myImpl = MyImpl()

    interface MyInteface {
        companion object {
           val interfaceValue = 10
        }
    }

    internal inner class MyImpl : MyInteface

    internal fun testing() {
        val getInterfaceValue = MyInteface.interfaceValue
    }
}

A third way would be to copy the value of interfaceValue into the implementing class: 第三种方法是将interfaceValue的值复制到实现类中:

class Experimenting {

    internal var myImpl = MyImpl()

    interface MyInteface {
        companion object {
            const val interfaceValue = 10
        }
    }

    internal inner class MyImpl : MyInteface{
        val interfaceValue = MyInteface.interfaceValue
    }

    internal fun testing() {
        val getInterfaceValue = myImpl.interfaceValue
    }
}

Basically you access it like you would access a static variable in java. 基本上,您就像访问Java中的静态变量一样访问它。

I guess the right conversion of such Java code to Kotlin code should be as below 我猜这种Java代码到Kotlin代码的正确转换应该如下

class Experimenting {

    internal var myImpl = MyImpl()

    interface MyInteface {

        val interfaceValue: Int
        get() = 10
    }

    internal inner class MyImpl : MyInteface

    internal fun testing() {
        val getInterfaceValue = myImpl.interfaceValue
    }
}

Though syntactically they are different, ie interfaceValue not really a static variable anymore, but practicality the same. 尽管在语法上它们是不同的,即interfaceValue不再是真正的静态变量,但实用性相同。

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

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