我不明白文档:
包级函数
在包 org.foo.bar 内的文件 example.kt 中声明的所有函数和属性,包括扩展函数,都被编译为名为 org.foo.bar.ExampleKt 的 Java 类的静态方法。
// example.kt
package demo
class Foo
fun bar() {
}
// Java
new demo.Foo();
demo.ExampleKt.bar();
我的代码如下。
编译错误;构建失败:
thufir@dur:~/NetBeansProjects/kotlin$
thufir@dur:~/NetBeansProjects/kotlin$ gradle compileJava
> Task :compileJava
/home/thufir/NetBeansProjects/kotlin/src/main/java/net/bounceme/dur/kotlin/App.java:12: error: package demo does not exist
new demo.Foo();
^
/home/thufir/NetBeansProjects/kotlin/src/main/java/net/bounceme/dur/kotlin/App.java:13: error: package demo does not exist
demo.ExampleKt.bar();
^
2 errors
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
* Get more help at https://help.gradle.org
BUILD FAILED in 0s
1 actionable task: 1 executed
thufir@dur:~/NetBeansProjects/kotlin$
java源码:
package net.bounceme.dur.kotlin;
import java.util.logging.Logger;
public class App {
private static final Logger LOG = Logger.getLogger(App.class.getName());
private void run() {
LOG.info("running");
new demo.Foo();
demo.ExampleKt.bar();
}
public static void main(String[] args) {
new App().run();
}
}
科特林来源:
package demo;
class Foo
fun bar() {
}
项目:
thufir@dur:~/NetBeansProjects/kotlin$
thufir@dur:~/NetBeansProjects/kotlin$ tree
.
├── build
│ ├── classes
│ │ └── java
│ │ └── main
│ └── tmp
│ └── compileJava
├── build.gradle
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
├── main
│ ├── java
│ │ └── net
│ │ └── bounceme
│ │ └── dur
│ │ └── kotlin
│ │ └── App.java
│ └── kotlin
│ └── example.kt
└── test
└── java
└── AppTest.java
18 directories, 9 files
thufir@dur:~/NetBeansProjects/kotlin$