Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

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

TakuSemba

July 19, 2017
Tweet

More Decks by TakuSemba

Other Decks in Technology

Transcript

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

    //kotlin class User { val name: String = "taku" }
  2. //java User user = new User(); user.getName(); // taku //kotlin

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

    getName(): String { return "my name is $name" } } //java User user = new User(); user.getName(); // taku
  4. //kotlin class User { val name: String = "taku" fun

    getName(): String { return "my name is $name" } } //java User user = new User(); user.getName(); // taku
  5. //kotlin class User { @JvmField val name: String = "taku"

    fun getName(): String { return "my name is $name" } } //java User user = new User(); user.getName(); // taku
  6. //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
  7. //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
  8. //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
  9. //kotlin class User { companion object { fun callName(name: String):

    String { return "hi, $name" } } //java User.Companion.callName("taku");
  10. //kotlin class User { companion object { @JvmStatic fun callName(name:

    String): String { return "hi, $name" } } //java User.Companion.callName("taku");
  11. //kotlin class User { companion object { @JvmStatic fun callName(name:

    String): String { return "hi, $name" } } //java User.callName("taku");
  12. //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();
  13. //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();
  14. //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;
  15. //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“) }
  16. //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);
  17. //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“) }
  18. //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“) }
  19. //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") }
  20. //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") }
  21. //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() }
  22. //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() }
  23. //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; });
  24. //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; });
  25. //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") }
  26. //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
  27. //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") }
  28. //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);
  29. //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