kotlin 调用对应java实现
1.枚举 kotlion默认是public
cotlin 枚举
enum class DoorState {
OPEN,
CLOSE
}
--------- java实现
public static enum DoorState {
OPEN,
CLOSE;
}
2. class
2.1 data class kotlin调用
data class LoginResult(变量)
----------java实现
public final class LoginResult { 对应变量 get set方法等}
2.2 open class kotlin调用 ,open表示可继承
open class MyOpenClass
----------java实现
public class MyOpenClass
2.3 class kotlin调用
class MyClass
----------java实现
public static final class MyClass
2.4 sealed class kotlin调用
sealed class Result
----------java实现
public abstract static class Result
2.5 object 类 可以直接取到类对象
object MyBroadCast
fun registerPicChangeReceiver(context: Context)
----------java实现
public final class MyBroadCast
static {
MyBroadCast var0 = new MyBroadCast();
INSTANCE = var0;
TAG = "MyBroadCast";
}
2.6 object 实现匿名接口 调用
mBtnTest.addOnAttachStateChangeListener(object :OnAttachStateChangeListener{...})
----------java实现
addOnAttachStateChangeListener((View.OnAttachStateChangeListener)(new View.OnAttachStateChangeListener() {...})
3.when
kotlin调用
when (aa) {
"11" -> {
Log.i(TAG, "test() when aa is 11")
} else -> {
Log.i(TAG, "test() when aa is not 11")
}
}
----------java实现
switch (aa) {
case "11":
Log.i(this.TAG, "test() when aa is 11");
break;
default:
Log.i(this.TAG, "test() when aa is not 11");
}
4.抽象类
abstract class absClass {
abstract fun fun1()
}
----------java实现
public abstract static class absClass {
public abstract void fun1();
}
5.接口
interface MyInterface {
fun bar()
}
----------java实现
public interface MyInterface {
void bar();
}
6.vararg 方法参数 是可变变量
fun testVararg(vararg nums: Int)
----------java实现
public final void testVararg(int... nums)