java - 如何在 kotlin 的另一个类中使用函数?
问题描述
我是 Kotlin 的新手。我有课,我想在另一个课上调用函数我尝试使用答案,但它给了我expecting member declaration error
我怎样才能使用它?
这是我的课
class MyClass() {
//Do stafff
val msg="Text"
MyObject.sendMsg("msg") //getting here error
}
//Another file i have this object file
object MyObject{
fun sendMsg(msg:String){
println("I get your message $msg")
}
}
解决方案
您不能仅在类主体中执行代码。例如,您可以声明一个方法
class MyClass() {
//Do stafff
val msg="Text"
fun test() {
MyObject.sendMsg("msg") //getting here error
}
}
推荐阅读
- symfony4 - 如何在 Symfony 项目中安装 Mercure?
- visual-c++ - 为 CListBox 太宽的项目显示工具提示
- c# - Rider 找不到已安装的 .NET Core,无法使用 MSBuild.dll 访问目录
- css - 无法使用优化的字体作为 CSS 内容
- android - 在构建应用程序时收到关于我的 Kotlin 版本的警告
- angular - 如何在 Excel 中安装使用 Angular 制作的 Addin Excel
- docker - dpkg 在 ubuntu-docker arm64 上安装错误
- r - R中按操作分组中最常见的因素
- azure-devops - Whitesource Bolt Azure DevOps 插件需要很长时间(而且太健谈)
- python - 带有时区的 psycopg2 时间戳和将选定数据从一个数据库插入另一个数据库的问题