美文网首页
kotlin internal 关键字使用

kotlin internal 关键字使用

作者: 因为我的心 | 来源:发表于2021-08-02 16:43 被阅读0次

一、前言:

internal 修饰类的方法,表示这个类方法只适合当前module使用,如果其他module使用的话,会找不到这个internal方法或者报错。下面我们在moduleA创建一个类 Apple ,里面有两个输出的方法。

class Apple() {
      fun appleLog(){
            Log.i("debug=","appleLog")
        }
       internal fun appleInternalLog(){
            Log.i("debug=","appleInternalLog")
        }

}

然后在 moduleB 创建 kt 类,调用 Apple 的方法,发现只有appleLog方法可以调用,而appleInternalLog 方法则是不显示。

Apple().appleLog()

再来,我们在 moduleB 创建 java 类,调用 Apple 的方法 ,

void text(){
    new Apple().appleInternalLog$production_sources_for_module_arms();//报错,usage of kotlin internal declaration from different module
    new Apple().appleLog(); //正常。
}

总结
所以 internal 限制了跨 module 的方法的使用

相关文章

  • kotlin internal 关键字使用

    internal 修饰类的方法,表示这个类方法只适合当前module使用,如果其他module使用的话,会找不到这...

  • kotlin internal 关键字使用

    一、前言: internal 修饰类的方法,表示这个类方法只适合当前module使用,如果其他module使用的话...

  • kotlin关键字-internal

    参考解答[https://stackoverflow.com/questions/47902656/kotlin-...

  • Kotlin相关

    Kotlin安装 Android Studio Idea Kotlin使用 var关键字声明可变属性,val关键字...

  • Kotlin - internal 关键字简单理解

    个人理解:internal 可以修饰 '方法、变量、类... 'internal 直译为 内部的; 在kt中 in...

  • Kotlin 接口

    原文地址:Kotlin 接口 Kotlin 接口与 Java 8 类似,使用 interface 关键字定义接口,...

  • Kotlin 继承

    ## Kotlin 继承 Kotlin 的继承与 Java 类似,Java 使用 extends 关键字而 Kot...

  • Kotlin 类 接口

    1.1接口 kotlin中使用interface关键字而不是class来声明一个kotlin的接口。 kotlin...

  • Kotlin基础知识(二):关键字与操作符

    一、关键字 链接:关键字与操作符 - Kotlin 语言中文站Kotlin中的关键字,按其能否作为标识符及使用场景...

  • Kotlin 关键字 Reified

    推荐使用 Kotlin 关键字 Reifiedhttps://juejin.cn/post/68449038335...

网友评论

      本文标题:kotlin internal 关键字使用

      本文链接:https://www.haomeiwen.com/subject/rdljvltx.html