kotlin internal 关键字使用

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 的方法的使用

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。