1. 单独编译运行一个Kotlin文件
- New -> File
- 直接新增 main 方法,见如下

2. 参考 code
package com.tct.freeze.kotlinstudydemo
/**
* 为什么新增 kt 文件无法单独运行
*
* 这种相当于 new -> File 的写法
*
* 故Kotlin 中和Java 不一样,不需要将 main 方法放到类下
*/
fun main(args : Array<String>) {
println("Hello world")
}
/***
* 一种不能单独编译运行的写法
*
* 这种相当于 new -> Class 的写法
*
* 这样的写法同 Java 的写法,但是这样写无法单独运行
*/
class MainRun {
fun main(args : Array<String>) {
println("Hello world")
}
}
本文详细介绍了如何在Kotlin中独立编译和运行单个文件,对比了Java的写法,强调了Kotlin中main方法的使用方式,并提供了一个有效的示例。
2392

被折叠的 条评论
为什么被折叠?



