JavaScript 中调用 Kotlin 方法实例详解
Kotlin 编译器生成正常的 JavaScript 类,可以在 JavaScript 代码中自由地使用的函数和属性 。不过,你应该记住一些微妙的事情。
用独立的 JavaScript 隔离声明
为了防止损坏全局对象,Kotlin 创建一个包含当前模块中所有 Kotlin 声明的对象 。所以如果你把模块命名为 myModule,那么所有的声明都可以通过 myModule 对象在 JavaScript 中可用。例如:
fun foo() = "Hello"可以在 JavaScript...
项目基于idea 生成,主要是说明使用方法
环境准备项目结构
main.kt
fun main(args: Array<String>) { val user: User = User(); user.age=333; user.name="dalong" println("this is my user ${user.name} ${user.age}")}
User.java
/** * @author dalong */public class User { private String name; private int age;? public String getName() { return name; }? public void setName(St...
我有一个完全用Kotlin编写的库,包括其公共API.现在,该库的用户使用Java,这里的问题是返回类型为Unit的Kotlin函数未编译为返回类型为void.结果是,对于有效无效的方法,Java端始终必须返回Unit.INSTANCE.可以避免这种情况吗?
例:
Kotlin界面interface Foo{fun bar()
}Java实现class FooImpl implements Foo{// should be public void bar()public Unit bar(){ return Unit.INSTANCE // ^^ implementations should not be forced to...
动机:
在我们的Android项目中,我们进行了许多验证,例如str!= null&& !str.isEmpty(),所以我决定将它们重构为辅助方法.一会儿,我使用以下课程作为助手:public class StringUtil {public static boolean isNullOrEmpty(@Nullable String str) {return str == null || str.isEmpty();}
}问题:
我们已经有一个用Kotlin编写的字符串的帮助程序类(例如,String.kt).因此,尚不清楚有两个助手(一个在Java中,一个在Kotlin中).
我试过的
...
我在Android Studio中有一个Kotlin项目.我在Kotlin代码中调用Java接口中的静态方法.构建失败并出现错误,Calls to static methods in Java interfaces are prohibited in JVM target 1.6. Recompile with '-jvm-target 1.8'我在build.gradle中有以下内容,compileOptions {sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility JavaVersion.VERSION_1_8
}我还在Kotlin编译器设置中将Target JVM版本更改为1.8.仍然,构建会...
是否可以使用反射来访问对象的私有字段并在此字段上调用公共方法?
即class Hello {private World word
}class World {public BlaBlaBla foo()
}Hello h = new Hello()World world = reflect on the h// And then world.foo()解决方法:可以使用反射访问私有字段.下面的例子(都写在Kotlin中)表明它……
使用Java反射:val hello = Hello()
val f = hello::class.java.getDeclaredField("world")
f.isAccessible = true
val w = f.ge...
假设我们有一个Java静态方法://Java code
public static void printFoo() {System.out.println("foo");
}可以在Kotlin中调用该方法吗?解决方法:是的你可以.Java代码:public class MyJavaClass {public static void printFoo() {System.out.println("foo");}
}Kotlin代码:fun main(args: Array<String>) {MyJavaClass.printFoo()
}这么容易=)
我正在尝试修补新的Room库与RxJava配对.
我找到了一种方法来使用Single在后台线程上插入项目,如活动内部:Single.fromCallable { AppDatabase.getInMemoryDatabase(this).taskDao().insertAll(task) }.subscribeOn(Schedulers.newThread()).subscribe()现在,我有一个RecyclerView,其任务有一个复选框,您可以使用该复选框将项目标记为完成或不完整.我想要做的是每次选中/取消选中时更新项目.我将粘贴整个ViewHolder以完成,但请特别注...
1,包级别的函数All the functions and properties declared in a file example.kt inside a package org.foo.bar, including extension functions, are compiled into static methods of a Java class named org.foo.bar.ExampleKt// example.kt
package democlass Foofun bar() { ... }对应的java操作// Java
new demo.Foo();
demo.ExampleKt.bar();如果想给转换后的java类指定类名,可以用 @JvmName :@file:JvmName("DemoUtils")...
JSON : Placeholder
JSON : Placeholder (https://jsonplaceholder.typicode.com/) 是一个用于测试的 REST API 网站。
以下使用 RxJava2 + Retrofit2 调用该网站的 REST API,获取字符串以及 JSON 数据。GET /posts/1
GET /posts
POST /posts
PUT /posts/1
DELETE /posts/1所有 GET API 都返回JSON数据,格式(JSON-Schema)如下:
{"type":"object","properties": {"userId": {"type" : "integer"},"id": {"type" : "integer"},"t...
JSON : Placeholder
JSON : Placeholder (https://jsonplaceholder.typicode.com/) 是一个用于测试的 REST API 网站。
以下使用 RxJava2 + Retrofit2 调用该网站的 REST API,获取字符串以及 JSON 数据。GET /posts/1
GET /posts
POST /posts
PUT /posts/1
DELETE /posts/1所有 GET API 都返回JSON数据,格式(JSON-Schema)如下:
{"type":"object","properties": {"userId": {"type" : "integer"},"id": {"type" : "integer"},"t...