Java와 함께 사용하는 Kotlin

7589a5a8fec022e8af4e46525150a291?s=47 TaeHwan
July 17, 2017

Java와 함께 사용하는 Kotlin

7589a5a8fec022e8af4e46525150a291?s=128

TaeHwan

July 17, 2017
Tweet

Transcript

  1. Java৬ ೣԋ ࢎਊೞח Kotlin Taehwan

  2. Javaীࢲ Kotlin ࢎਊೡ ٸ • null ઱੄/3೦ध • JvmOverloads •

    Static Fields • Class ࢚ࣘ
  3. null ઱੄

  4. null ઱੄ // class name String.kt fun merge(a: String, b:

    String) = "$a $b"
  5. null ઱੄ - javaীࢲ ࠗܳ ٸ // class name String.kt

    fun merge(a: String, b: String) = "$a $b" public class Sample { @Test public void sample() { System.out.println("merge " + StringKt.merge("A", "B")); } }
  6. null ઱੄ - javaীࢲ ࠗܳ ٸ - value // class

    name String.kt fun merge(a: String, b: String) = "$a $b" public class Sample { private String a = "A"; private String b = "B"; @Test public void sample() { System.out.println("merge " + StringKt.merge(a, b)); } }
  7. null ઱੄ - javaীࢲ ࠗܳ ٸ - null value //

    class name String.kt fun merge(a: String, b: String) = "$a $b" public class Sample { private String a = "A"; private String b = "B"; @Before public void setUp() { a = null; } @Test public void sample() { System.out.println("merge " + StringKt.merge(a, b)); } }
  8. null ઱੄ - java ௏٘ value null // class name

    String.kt fun merge(a: String, b: String) = "$a $b" public class Sample { private String a = "A"; private String b = "B"; @Before public void setUp() { a = null; } @Test public void sample() { System.out.println("merge " + StringKt.merge(a, b)); // java.lang.IllegalArgumentException: // Parameter specified as non-null is null: // method tech.thdev.myapplication.StringKt.merge } }
  9. null ઱੄ - javaীࢲ ࠗܳ ٸ - null value //

    class name String.kt fun merge(a: String, b: String) = "$a $b" public class Sample { private String a = "A"; private String b = "B"; @Before public void setUp() { // a = null; } @Test public void sample() { a = null; System.out.println("merge " + StringKt.merge(a, b)); // ਕ׬ ݫद૑ : Argument 'a' might be null less... } }
  10. Ӓېࢲ • nullable ୊ܻ • ? ೞաݶ ୊ܻ оמ //

    class name String.kt fun merge(a: String?, b: String?) = "$a $b"
  11. प೯ೞݶ ׮਺୊ۢ • return : merge null B // class

    name String.kt fun merge(a: String?, b: String?) = "$a $b"
  12. null ୊ܻח? - if != null // class name String.kt

    fun merge(a: String?, b: String?) = { if (a != null && b != null) { "$a $b" } }
  13. null ୊ܻח? - if != null Ѿҗ • return :

    merge Function0<kotlin.Unit> // class name String.kt fun merge(a: String?, b: String?) = { if (a != null && b != null) { "$a $b" } }
  14. null ୊ܻח? - if != null Ѿҗ • return :

    // class name String.kt fun merge(a: String?, b: String?) = { if (a != null && b != null) { "$a $b" } else { "" } }
  15. 3೦ध

  16. 3೦ध • java String merge(String a, String b) { return

    (a != null ? a : "a") + " " + (b != null ? b : "b"); }
  17. 3೦ध • java String merge(String a, String b) { return

    (a != null ? a : "a") + " " + (b != null ? b : "b"); } • kotlin fun merge(a: String?, b: String?) = "${a != null ? a : "a"} ${b != null ? b : "b"}"
  18. 3೦ध • java String merge(String a, String b) { return

    (a != null ? a : "a") + " " + (b != null ? b : "b"); } • kotlin fun merge(a: String?, b: String?) = "${a != null ? a : "a"} ${b != null ? b : "b"}" ޙߨ য়ܨ ߊࢤ
  19. 3೦ध - if else ୊ܻೞӝ // class name String.kt fun

    merge(a: String?, b: String?) = "${if (a != null) a else "a"} ${if (b != null) b else "b"}"
  20. 3೦ध - if else ୊ܻೞӝ // class name String.kt fun

    merge(a: String?, b: String?) = "${if (a != null) a else "a"} ${if (b != null) b else "b"}" ӡযࢲ फযਃ. ઴ৈ઱ࣁਃ.
  21. 3೦ध - if else ୊ܻೞӝ // class name String.kt fun

    merge(a: String?, b: String?) = "${if (a != null) a else "a"} ${if (b != null) b else "b"}" // class name String.kt fun merge(a: String?, b: String?) = "${a ?: "a"} ${b ?: "b"}"
  22. null ୊ܻח? - ইې৬ э੉ ୊ܻ೤द׮ • return : merge

    a B // class name String.kt fun merge(a: String?, b: String?) = "${a ?: "a"} ${b ?: "b"}"
  23. Kotlinীࢲ Java ௏٘ ࢎਊೡ ٸח?

  24. Kotlinীࢲ Java ௏٘ ࢎਊೡ ٸח? • Annotationਸ ੜ ੸ਊೞ੗ •

    @Nullable/@NotNull
  25. JvmOverloads

  26. JvmOverloads class Sample( val name: String, val age: Int =

    0, val birthday: String = "2000-01-01") { init { println("name $name age $age birthday $birthday") } }
  27. JvmOverloads - javaীࢲ ࠗܰݶ

  28. JvmOverloads - javaীࢲ ೣԋ ࢎਊೞ۰ݶ? class Sample( val name: String,

    val age: Int = 0, val birthday: String = "2000-01-01") { constructor(name: String, age: Int): this(name, age, "2000-01-01") constructor(name: String): this(name, 0) init { println("name $name age $age birthday $birthday") } }
  29. JvmOverloads - javaীࢲ ೣԋ ࢎਊೞ۰ݶ?

  30. ӡয ӡয ӡয

  31. JvmOverloads - @JvmOverloads ୶оೞӝ • @JvmOverloads class Sample @JvmOverloads constructor(

    val name: String, val age: Int = 0, val birthday: String = "2000-01-01") { init { println("name $name age $age birthday $birthday") } }
  32. JvmOverloads - ѾҴ

  33. JvmOverloads - function @JvmOverloads fun f(a: String, b: Int =

    0, c: String = "abc") { ... }
  34. JvmOverloads - function ഐ୹ java void f(String a, int b,

    String c) { } void f(String a, int b) { } void f(String a) { }
  35. Static Fields

  36. Static Fields - final static ੿੄ೞӝ • const/@JvmField class Key

    { companion object { const val TYPE_ZERO = 0 @JvmField val TYPE_ONE = 1 } }
  37. Static Fields class Key { companion object { const val

    TYPE_ZERO = 0 @JvmField val TYPE_ONE = 1 } } // Java class SampleTest { @Test public void sample() { System.out.println(Key.TYPE_ZERO); System.out.println(Key.TYPE_ONE); } }
  38. Class ࢚ࣘ

  39. Class ࢚ࣘ - Java public class TestOne { public void

    print() { System.out.println("TestOne"); } }
  40. Class ࢚ࣘ - Java > Kotlin public class TestOne {

    public void print() { System.out.println("TestOne"); } } class TestTwo : TestOne() { override fun print() { super.print() print("TestTwo") } }
  41. Class ࢚ࣘ - Java > Kotlin > Java public class

    TestOne { public void print() { System.out.println("TestOne"); } } class TestTwo : TestOne() { override fun print() { super.print() print("TestTwo") } } public class TestThree extends TestTwo { }
  42. Class ࢚ࣘ

  43. Class ࢚ࣘ - Error?? • Kotlin ௿ېझח ӝࠄ੸ਵ۽ abstract/interfaceо ইפݶ

    ࢚ࣘ੉ ࠛо • open੉ۄח ఃਕ٘ܳ ݺद੸ਵ۽ ੸যષਵ۽ॄ ࢚ࣘ੉ оמ
  44. Class ࢚ࣘ open class TestTwo : TestOne() { override fun

    print() { super.print() print("TestTwo") } } public class TestThree extends TestTwo { override fun print() { super.print() print("TestThree") } }
  45. End... RGP Koreaীࢲ উ٘۽੉٘ ѐߊ੗ܳ ଺Ҋ ੓ যਃ! kotlin ೣԋ

    ೞप ࠙!