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

JavaからみたKotlin

 JavaからみたKotlin

Amane Nikaido

October 17, 2017
Tweet

More Decks by Amane Nikaido

Other Decks in Programming

Transcript

  1. 0CKFDU object KotlinObject { fun hello() : String { return

    "Hello Kotlin!" } fun hello2() = "Hello Kotlin!" } ˞γϯάϧτϯͰ͢ ,PUMJOͷ0CKFDU ˞IFMMP ͱIFMMP ͸CZUFDPEFʹ͢Δͱશ͘ಉ͡Ͱ͢
  2. 0CKFDU import jp.a2kaido.skfj.KotlinObject as KO class KotlinObjectFromKotlinTest { @Test fun

    KotlinObject() { Assert.assertEquals("Hello Kotlin!", KO.hello()) Assert.assertEquals("Hello Kotlin!", KO.hello2()) } } ,PUMJO͔Βݟͨ৔߹
  3. 0CKFDU public class KotlinObjectFromJavaTest { @Test public void KotlinObject() {

    Assert.assertEquals("Hello Kotlin!", KotlinObject.INSTANCE.hello()); Assert.assertEquals("Hello Kotlin!", KotlinObject.INSTANCE.hello2()); } } +BWB͔Βݟͨ৔߹
  4. 0CKFDU public class KotlinObjectFromJavaTest { @Test public void KotlinObject() {

    Assert.assertEquals("Hello Kotlin!", KotlinObject.INSTANCE.hello()); Assert.assertEquals("Hello Kotlin!", KotlinObject.INSTANCE.hello2()); } } +BWB͔Βݟͨ৔߹ ˞*/45"/$&ʜ
  5. 0CKFDU ,PUMJOͷ0CKFDU object KotlinObject2 { @JvmStatic fun hello() : String

    { return "Hello Kotlin!" } @JvmStatic fun hello2() = "Hello Kotlin!" }
  6. 0CKFDU +BWB͔Βݟͨ৔߹ public class KotlinObjectFromJavaTest { @Test public void KotlinObject()

    { Assert.assertEquals("Hello Kotlin!", KotlinObject.INSTANCE.hello()); Assert.assertEquals("Hello Kotlin!", KotlinObject.INSTANCE.hello2()); } @Test public void KotlinObject2() { Assert.assertEquals("Hello Kotlin!", KotlinObject2.hello()); Assert.assertEquals("Hello Kotlin!", KotlinObject2.hello2()); } } ,PUMJO0CKFDUHFU*OTUBODF IFMMP ͱॻ͖͍ͨ
  7. 5PQMFWFMGVODUJPO +BWB͔Βݟͨ৔߹ public class KotlinFunctionFromJavaTest { @Test public void KotlinFunction()

    { Assert.assertEquals("Hello Kotlin!", KotlinFunctionKt.hello()); } } ,PUMJO'VODUJPO,Uͱ͍͏Ϋϥε͕Ͱ͖ͯΔ ͦͷΫϥεʹTUBUJDϝιου͕ੜ͑ͯΔ
  8. 5PQMFWFMGVODUJPO ,PUMJOͷ5PQMFWFMGVODUJPO fun hello() : String { return "Hello Kotlin!"

    } ,PUMJO'VODUJPOLU ,PUMJO'VODUJPO,Uͱ͍͏໊લ͕ؾ࣋ͪѱ͍ͷͰɺ
  9. 5PQMFWFMGVODUJPO +BWB͔Βݟͨ৔߹ public class KotlinFunctionFromJavaTest { @Test public void KotlinFunction()

    { Assert.assertEquals("Hello Kotlin!", KotlinFunctionKt.hello()); } @Test public void KotlinObjectRefine() { Assert.assertEquals("Hello Kotlin!", KotlinFunctionRefine.helloRefine()); } } ,Uͷ··ͩͱؾ࣋ͪѱ͍ͷͰΞϊςʔγϣϯͰ
 ໊લΛ͚ͭͯ͋͛·͠ΐ͏ +BWB͔ΒͰ΋໰୊ͳ͘ݺͼग़ͤΔʂ
  10. &YUFOTJPO ,PUMJOͷ&YUFOTJPO fun String.hello() = "Hello Kotlin!" 4USJOH&YUFOTJPOLU @file:JvmName("String") package

    jp.a2kaido.skfj.extension fun String.helloRefine() = "Hello Kotlin!" 4USJOH&YUFOTJPO3FpOFLU
  11. &YUFOTJPO ,PUMJO͔Βݟͨ৔߹ class StringExtensionFromKotlinTest { @Test fun StringExtension() { Assert.assertEquals("Hello

    Kotlin!", "hoge".hello()) } @Test fun StringExtensionRefine() { Assert.assertEquals("Hello Kotlin!", "hoge".helloRefine()) } }
  12. &YUFOTJPO +BWB͔Βݟͨ৔߹ public class StringExtensionFromJavaTest { @Test public void StringExtension()

    { Assert.assertEquals("Hello Kotlin!", StringExtensionKt.hello("hoge")); } @Test public void StringExtensionRefile() { Assert.assertEquals("Hello Kotlin!", String.helloRefine("hoge")); } } 4USJOH&YUFOTJPO,Uͱ͍͏Ϋϥε͕Ͱ͖ͯΔ ϨγʔόʔΛҾ਺Ͱड͚औΔ ݺͼग़ͤΔ͚Ͳʜ&YUFOTJPOͷ͝ར༻͸ܭըతʹ
  13. %FGBVMU"SHVNFOU ,PUMJOͷ%FGBVMU"SHVNFOU class KotlinDefaultArgument { fun hello(s : String =

    "Kotlin") = "Hello $s!" } class KotlinDefaultArgumentRefine { @JvmOverloads fun hello(s : String = "Kotlin") = "Hello $s!" }
  14. %FGBVMU"SHVNFOU ,PUMJO͔Βݟͨ৔߹ class KotlinDefaultArgumentFromKotlinTest { @Test fun kotlinDefaultArgument() { val

    target = KotlinDefaultArgument() Assert.assertEquals("Hello Kotlin!", target.hello()) Assert.assertEquals("Hello Java!", target.hello("Java")) } @Test fun kotlinDefaultArgumentRefine() { val target = KotlinDefaultArgumentRefine() Assert.assertEquals("Hello Kotlin!", target.hello()) Assert.assertEquals("Hello Java!", target.hello("Java")) } }
  15. %FGBVMU"SHVNFOU +BWB͔Βݟͨ৔߹ public class KotlinDefaultArgumentFromJavaTest { @Test public void KotlinDefaultArgument()

    { KotlinDefaultArgument target = new KotlinDefaultArgument(); // cannot compile // Assert.assertEquals("Hello Kotlin!", target.hello()); Assert.assertEquals("Hello Java!", target.hello("Java")); } @Test public void KotlinDefaultArgumentRefine() { KotlinDefaultArgumentRefine target = new KotlinDefaultArgumentRefine(); Assert.assertEquals("Hello Kotlin!", target.hello()); Assert.assertEquals("Hello Java!", target.hello("Java")); } } @JvmOverloadsΛ͚ͭͳ͍ͱҾ਺ͳ͠ͷݺͼग़͕͠Ͱ͖ͳ͍
  16. %FGBVMU*NQMFNFOUBUJPO public class DefaultInterfaceJavaImpl implements DefaultInterface { @NotNull @Override public

    String hello() { // ࣮૷ඞਢ return DefaultImpls.hello(this); // DefaultImpls͕ੜ͑ͯΔ } @NotNull @Override public String hello2() { return "Hello Kotlin!"; } } +BWBͰ࣮૷ ஌Βͳ͍ͱແཧήʔɻσϑΥϧτΛແࢹ࣮ͯ͠૷ͦ͠͏ɻ