kotlin 调用对应java实现
一. 类class
1 data class kotlin调用,必须要有带参数的构造方法,
如果调用init方法则在带参数的构造方法中添加init 内的代码。可以添加constructor构造方法添加次构造方法,次构造方法需要通过this调用主构造方法。
格式:data class LoginResult(变量) 例如:
data class LoginResult(
val success: LoggedInUserView? = null,
val error: Int? = null
) {
// init {
// println("test init")
// }
// constructor( error: Int? = null) : this(null , error){
// println("test init")
// }
}
----------java实现
public final class LoginResult { 对应变量 get方法,带参构造方法等} 例如:
public final class LoginResult {
@Nullable
private final LoggedInUserView success;
@Nullable
private final Integer error;
@Nullable
public final LoggedInUserView getSuccess() {
return this.success;
}
@Nullable
public final Integer getError() {
return this.error;
}
public LoginResult(@Nullable LoggedInUserView success, @Nullable Integer error) {
this.success = success;
this.error = error;
}
}
2 open class kotlin调用 ,open表示可继承
open class MyOpenClass
----------java实现
public class MyOpenClass
3 class kotlin调用
class MyClass
----------java实现
public static final class MyClass
4 sealed class kotlin调用
sealed class Result
----------java实现
public abstract static class Result
ps:sealed class 和abstract class区别是 sealed class子类可以定在密封类外部,但是必须在同一文件中,abstract class子类定义没有要求
5 object 类 可以直接取到类单例对象且类似java final修饰
例如:MyBroadCast.registerPicChangeReceiver(this)
object MyBroadCast {
fun registerPicChangeReceiver(context: Context) {
}
}
----------java实现
public final class MyBroadCast {
@NotNull
public static final MyBroadCast INSTANCE;
public final void registerPicChangeReceiver(@NotNull Context context) {
Intrinsics.checkNotNullParameter(context, "context");
}
private MyBroadCast() {
}
static {
MyBroadCast var0 = new MyBroadCast();
INSTANCE = var0;
}
}
5.1 如果是普通class 创建单例 可以使用如下kotlin get方法创建
companion object {
private const val TAG = "TestClass"
private var mInstance: TestClass? = null
@get:Synchronized
val ins: TestClass?
get() {
//sonarqube suggestion
if (mInstance == null) {
mInstance = TestClass()
}
return mInstance
}
}
或者kotlin fun 方法实现
companion object {
private const val TAG = "TestClass"
private var mInstance: TestClass? = null
fun getIns():TestScenesUtil {
if (mInstance == null) {
mInstance = TestScenesUtil()
}
return mInstance as TestScenesUtil
}
}
----------java实现
public static synchronized TestClass getIns() {
if (mInstance == null) {
mInstance = new TestClass();
}
return mInstance;
}
6 object 实现匿名接口 调用
mBtnTest.addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
override fun onViewAttachedToWindow(view: View) {}
override fun onViewDetachedFromWindow(view: View) {}
})
----------java实现
mBtnTest.addOnAttachStateChangeListener((View.OnAttachStateChangeListener)(new View.OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View v) {
}
@Override
public void onViewDetachedFromWindow(View v) {
}}));
7 companion object 类被加载时就初始化变量 类似java static final 修饰
companion object {
private const val TAG = "app_TestActivity"
}
----------java实现
private static final String TAG = "app_TestActivity";
8.枚举 kotlion默认是public
cotlin 枚举
enum class DoorState {
OPEN,
CLOSE
}
--------- java实现
public static enum DoorState {
OPEN,
CLOSE;
}
9.抽象类
abstract class absClass {
abstract fun fun1()
}
----------java实现
public abstract static class absClass {
public abstract void fun1();
}
10.接口
interface MyInterface {
fun bar()
}
----------java实现
public interface MyInterface {
void bar();
}
二.关键字
1.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");
}
2.vararg 方法参数 是可变变量
fun testVararg(vararg nums: Int)
----------java实现
public final void testVararg(int... nums)
3.for循环
for (i in 0 until naviRoadChildCount) {
}
----------java实现
for (int i = 0; i < serviceAreaChildCount; i++) {
}
4.类型判断 instanceof
if (mBtn is Button) {
}
----------java实现
if (mBtn instanceof Button) {
}
5.fun方法声明,默认是public 类型,如果是私有类型需要加private修饰
fun setVolume(volumeIndex: Int, volume: Int) {
}
----------java实现
public void setVolume(int volumeIndex, int volume) {
}
6. get set方法声明,如下kotlin实现 调用时候直接调用 class.state 进行读取或者赋予值,
如果java实现 getState带参数,或者 setState带大于1个参数,则kotlin转换成 fun 方法。
var state: Boolean
get() {
val Opened:Boolean = false
return Opened
}
set(isOn) {
Logger.i(TAG, "setState isOn:$isOn")
}
----------java实现
public boolean getState() {
boolean Opened = false;
return Opened;
}
public void setState(boolean isOn) {
Logger.i(TAG, "setState isOn:" + isOn);
}
7.类型转换
mRoadInfo as RoadInfo
----------java实现
(RoadInfo) mRoadInfo