How Java and Kotlin work together @ca.kt#2

How Java and Kotlin work together @ca.kt#2

A374f41eab3f73c50d8bab0652cb207a?s=128

TakuSemba

July 19, 2017
Tweet

Transcript

  1. 5.
  2. 6.

    //java User user = new User(); user.setName("taku"); user.getName(); // taku

    //kotlin class User { val name: String = "taku" }
  3. 7.

    //java User user = new User(); user.getName(); // taku //kotlin

    class User { val name: String = "taku" }
  4. 8.

    //kotlin class User { val name: String = "taku" fun

    getName(): String { return "my name is $name" } } //java User user = new User(); user.getName(); // taku
  5. 9.

    //kotlin class User { val name: String = "taku" fun

    getName(): String { return "my name is $name" } } //java User user = new User(); user.getName(); // taku
  6. 10.

    //kotlin class User { @JvmField val name: String = "taku"

    fun getName(): String { return "my name is $name" } } //java User user = new User(); user.getName(); // taku
  7. 11.

    //kotlin class User { @JvmField val name: String = "taku"

    fun getName(): String { return "my name is $name" } } //java User user = new User(); String name = user.name; // taku user.getName(); // my name is taku
  8. 12.

    //kotlin class User { @JvmField val name: String = "taku"

    fun getName(name: String = "semba"): String { return "my name is $name" } } //java User user = new User(); String name = user.name; // taku user.getName(); // my name is taku
  9. 13.

    //kotlin class User { @JvmField val name: String = "taku"

    @JvmOverloads fun getName(name: String = "semba"): String { return "my name is $name" } } //java User user = new User(); String name = user.name; // taku user.getName(); // my name is semba
  10. 14.

    //kotlin class User { companion object { fun callName(name: String):

    String { return "hi, $name" } } //java User.Companion.callName("taku");
  11. 15.

    //kotlin class User { companion object { @JvmStatic fun callName(name:

    String): String { return "hi, $name" } } //java User.Companion.callName("taku");
  12. 16.

    //kotlin class User { companion object { @JvmStatic fun callName(name:

    String): String { return "hi, $name" } } //java User.callName("taku");
  13. 17.

    //kotlin class User { companion object { val name: String

    = "taku" @JvmStatic fun callName(name: String): String { return "hi, $name" } } //java User.callName("taku"); String name = User.Companion.getName();
  14. 18.

    //kotlin class User { companion object { const val name:

    String = "taku" @JvmStatic fun callName(name: String): String { return "hi, $name" } } //java User.callName("taku"); String name = User.Companion.getName();
  15. 19.

    //kotlin class User { companion object { const val name:

    String = "taku" @JvmStatic fun callName(name: String): String { return "hi, $name" } } //java User.callName("taku"); String name = User.name;
  16. 21.

    //MainActivity.kt val sp = getPreferences(Context.MODE_PRIVATE) val userId = sp.getUserId() //prefExt.kt

    fun SharedPreferences.getUserId(): String { return getString("userId", null) ?: throw IllegalStateException(“user id does not exist“) }
  17. 23.

    //prefExt.kt fun SharedPreferences.getUserId(): String { return getString("userId", null) ?: throw

    IllegalStateException(“user id does not exist“) } //MainActivity.java SharedPreferences sp = getPreferences(Context.MODE_PRIVATE); String userId = PrefExtKt.getUserId(sp);
  18. 24.

    //MainActivity.java SharedPreferences sp = getPreferences(Context.MODE_PRIVATE); String userId = PrefExtKt.getUserId(sp); //prefExt.kt

    @file:JvmName("PrefUtil") fun SharedPreferences.getUserId(): String { return getString("userId", null) ?: throw IllegalStateException(“user id does not exist“) }
  19. 25.

    //MainActivity.java SharedPreferences sp = getPreferences(Context.MODE_PRIVATE); String userId = PrefUtil.getUserId(sp); //prefExt.kt

    @file:JvmName("PrefUtil") fun SharedPreferences.getUserId(): String { return getString("userId", null) ?: throw IllegalStateException(“user id does not exist“) }
  20. 27.

    //prefExt.kt fun SharedPreferences.edit(func: (SharedPreferences.Editor) -> Unit) { val editor =

    edit() func(editor) editor.apply() } //MainActivity.kt val sp = getPreferences(Context.MODE_PRIVATE) sp.edit { editor -> editor.putString("key", "value") }
  21. 28.

    //prefExt.kt fun SharedPreferences.edit(func: (SharedPreferences.Editor) -> Unit) { val editor =

    edit() func(editor) editor.apply() } //MainActivity.kt val sp = getPreferences(Context.MODE_PRIVATE) sp.edit { it.putString("key", "value") }
  22. 29.

    //MainActivity.kt val sp = getPreferences(Context.MODE_PRIVATE) sp.edit { it.putString("key", "value") }

    //prefExt.kt fun SharedPreferences.edit(func: SharedPreferences.Editor.() -> Unit) { val editor = edit() editor.func() editor.apply() }
  23. 30.

    //MainActivity.kt val sp = getPreferences(Context.MODE_PRIVATE) sp.edit { putString("key", "value") }

    //prefExt.kt fun SharedPreferences.edit(func: SharedPreferences.Editor.() -> Unit) { val editor = edit() editor.func() editor.apply() }
  24. 32.

    //prefExt.kt fun SharedPreferences.edit(func: SharedPreferences.Editor.() -> Unit) { val editor =

    edit() editor.func() editor.apply() } //MainActivity.java PrefExtKt.edit(sp, editor -> { editor.putString("key", "value"); return Unit.INSTANCE; });
  25. 33.

    //prefExt.kt inline fun SharedPreferences.edit(func: SharedPreferences.Editor.() -> Unit) { val editor

    = edit() editor.func() editor.apply() } //MainActivity.java PrefExtKt.edit(sp, editor -> { editor.putString("key", "value"); return Unit.INSTANCE; });
  26. 35.

    //prefExt.kt inline fun <reified T> SharedPreferences.get(key: String, default: T): T

    { if (default is String) return getString(key, default) as T if (default is Int) return getInt(key, default) as T if (default is Float) return getFloat(key, default) as T if (default is Long) return getLong(key, default) as T if (default is Boolean) return getBoolean(key, default) as T throw IllegalStateException("failed to get value") }
  27. 36.

    //prefExt.kt inline fun <reified T> SharedPreferences.get(key: String, default: T): T

    { if (default is String) return getString(key, default) as T if (default is Int) return getInt(key, default) as T if (default is Float) return getFloat(key, default) as T if (default is Long) return getLong(key, default) as T if (default is Boolean) return getBoolean(key, default) as T throw IllegalStateException("failed to get value") } //MainActivity.kt val sp = getPreferences(Context.MODE_PRIVATE) val a= sp.get("key", 0L) // return as Long val b = sp.get("key", "my default") // return as String
  28. 37.

    //prefExt.kt inline fun <reified T> SharedPreferences.get(key: String, default: T): T

    { if (default is String) return getString(key, default) as T if (default is Int) return getInt(key, default) as T if (default is Float) return getFloat(key, default) as T if (default is Long) return getLong(key, default) as T if (default is Boolean) return getBoolean(key, default) as T throw IllegalStateException("failed to get value") }
  29. 38.

    //prefExt.kt inline fun <reified T> SharedPreferences.get(key: String, default: T): T

    { if (default is String) return getString(key, default) as T if (default is Int) return getInt(key, default) as T if (default is Float) return getFloat(key, default) as T if (default is Long) return getLong(key, default) as T if (default is Boolean) return getBoolean(key, default) as T throw IllegalStateException("failed to get value") } //MainActivity.java SharedPreferences sp = getPreferences(Context.MODE_PRIVATE); PrefExtKt.get(sp, "key", 0L);
  30. 39.

    //prefExt.kt inline fun <reified T> SharedPreferences.get(key: String, default: T): T

    { if (default is String) return getString(key, default) as T if (default is Int) return getInt(key, default) as T if (default is Float) return getFloat(key, default) as T if (default is Long) return getLong(key, default) as T if (default is Boolean) return getBoolean(key, default) as T throw IllegalStateException("failed to get value") } //MainActivity.java SharedPreferences sp = getPreferences(Context.MODE_PRIVATE); PrefExtKt.get(sp, "key", 0L); // get() has a private access in PrefExtKt