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

Java와 함께 사용하는 Kotlin

TaeHwan
July 17, 2017

Java와 함께 사용하는 Kotlin

TaeHwan

July 17, 2017
Tweet

More Decks by TaeHwan

Other Decks in Technology

Transcript

  1. 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")); } }
  2. 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)); } }
  3. 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)); } }
  4. 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 } }
  5. 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... } }
  6. Ӓېࢲ • nullable ୊ܻ • ? ೞաݶ ୊ܻ оמ //

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

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

    fun merge(a: String?, b: String?) = { if (a != null && b != null) { "$a $b" } }
  9. 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" } }
  10. null ୊ܻח? - if != null Ѿҗ • return :

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

    (a != null ? a : "a") + " " + (b != null ? b : "b"); }
  12. 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"}"
  13. 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"}" ޙߨ য়ܨ ߊࢤ
  14. 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"}"
  15. 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"}" ӡযࢲ फযਃ. ઴ৈ઱ࣁਃ.
  16. 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"}"
  17. null ୊ܻח? - ইې৬ э੉ ୊ܻ೤द׮ • return : merge

    a B // class name String.kt fun merge(a: String?, b: String?) = "${a ?: "a"} ${b ?: "b"}"
  18. JvmOverloads class Sample( val name: String, val age: Int =

    0, val birthday: String = "2000-01-01") { init { println("name $name age $age birthday $birthday") } }
  19. 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") } }
  20. 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") } }
  21. JvmOverloads - function ഐ୹ java void f(String a, int b,

    String c) { } void f(String a, int b) { } void f(String a) { }
  22. Static Fields - final static ੿੄ೞӝ • const/@JvmField class Key

    { companion object { const val TYPE_ZERO = 0 @JvmField val TYPE_ONE = 1 } }
  23. 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); } }
  24. Class ࢚ࣘ - Java public class TestOne { public void

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

    public void print() { System.out.println("TestOne"); } } class TestTwo : TestOne() { override fun print() { super.print() print("TestTwo") } }
  26. 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 { }
  27. Class ࢚ࣘ - Error?? • Kotlin ௿ېझח ӝࠄ੸ਵ۽ abstract/interfaceо ইפݶ

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

    print() { super.print() print("TestTwo") } } public class TestThree extends TestTwo { override fun print() { super.print() print("TestThree") } }