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

新言語KotlinでAndroidプログラミング #DroidKaigi

新言語KotlinでAndroidプログラミング #DroidKaigi

DroidKaigiでの発表で使用したスライドです。

Taro Nagasawa

April 25, 2015
Tweet

More Decks by Taro Nagasawa

Other Decks in Programming

Transcript

  1. Hello World (Java) class HelloWorld { public static void main(String[]

    args) { System.out.println("Hello, world!"); } }
  2. Hello World (Kotlin) fun main(args: Array<String>) { println("Hello, world!") }

    τοϓϨϕϧʹ ؔ਺Λஔ͚Δ ഑ྻʹ δΣωϦΫε ηϛίϩϯ ෆཁ
  3. Ϋϥε (Java) class User { private final long id; private

    final String name; public User(long id, String name) { this.id = id; this.name = Objects.requireNonNull(name); } public long getId() { return id; } public String getName() { return name; } / / toString, equals, hashCodeͱ͔ }
  4. Ϋϥε (Kotlin) class User(val id: Long, val name: String) {

    / / toString, equals, hashCodeͱ͔ } ίϯετϥΫλҾ਺͕ ͦͷ··ϓϩύςΟʹ
  5. σʔλΫϥε val taro = User(123, "Taro") println(taro.id) / / =>

    123 println(taro.name) / / => Taro println(taro) / / => User(id=123, name=Taro)
  6. ؔ਺ܕΠϯλϑΣʔε (Java) / / anonymous class button.setOnClickListener(new View.OnClickListener() { @Override

    public void onClick(View v) { toast("Clicked!"); } }); ! / / lambda button.setOnClickListener(v -> toast("Clicked!"));
  7. ߴ֊ؔ਺ͰίϨΫγϣϯૢ࡞ (1..999) .filter { it % 3 == 0 ||

    it % 5 == 0 } .reduce { a, b -> a + b } ʮΠϯϥΠϯؔ਺ʯ ؔ਺Ϧςϥϧ͕ల։͞Ε ΦϒδΣΫτΛੜ੒͠ͳ͍
  8. ಠࣗศརϝιου (Java) / / ఆٛ static void toast(Context c, String

    s) {...} ! / / ݺͼग़͠ MyUtils.toast(context, "Hello");
  9. ಠࣗศརϝιου (Kotlin) / / ఆٛ fun Context.toast(s: String) {...} !

    / / ݺͼग़͠ context.toast("Hello") ! / / ݺͼग़͠(ActivityͳͲͷத) toast("Hello")
  10. ಠࣗศརϝιου (Kotlin) / / ఆٛ fun Context.toast(s: String) {...} !

    / / ݺͼग़͠ context.toast("Hello") ! / / ݺͼग़͠(ActivityͳͲͷத) toast("Hello") ϝιουΛੜ΍ͤΔ ʮ֦ுؔ਺ʯ ੩తʹղܾ͞ΕΔʂ
  11. Optional (Java) String a = "hoge"; Optional<String> b = Optional.of("fuga");

    Optional<String> c = null; ΦϒδΣΫτੜ੒ ୭ʹ΋OVMM͸ࢭΊΒΕͳ͍ʂ
  12. Optional (Kotlin) val a: String = "hoge" val b: String

    = null / / NG ! val c: String? = "hoge" val d: String? = null / / OK ! d.toUpperCase() / / NG
  13. NULL҆શ val a: String? = "hoge" val b: String? =

    null ! if (a != null) a.toUpperCase() else null / / => HOGE if (b != null) b.toUpperCase() else null / / => null ! a?.toUpperCase() / / => HOGE b?.toUpperCase() / / => null
  14. NULL҆શ val a: String? = "hoge" val b: String? =

    null ! if (a != null) a.toUpperCase() else null / / => HOGE if (b != null) b.toUpperCase() else null / / => null ! a?.toUpperCase() / / => HOGE b?.toUpperCase() / / => null OVMMͰͳ͍͜ͱ͕อূ͞Ε͍ͯΔ ৔ॴͰ͸/PU/VMMܕͱͯ͠ѻ͑Δ
  15. KotterKnife ࢖༻લ var button: Button? = null ! button =

    findViewById(R.id.button) as Button ! button?.setOnClickListener {...}
  16. KotterKnife ࢖༻લ var button: Button? = null ! button =

    findViewById(R.id.button) as Button ! button?.setOnClickListener {...} "DUJWJUZͱ͔ͷϓϩύςΟ ຖ౓ͷpOE7JFX#Z*E ͦͯ͠Ωϟετʜ
  17. KotterKnife ࢖༻ޙ var button: Button by bindView(R.id.button) ! button.setOnClickListener {...}

    ϓϩύςΟ ಉ͡ CZΩʔϫʔυʹଓ͚ͯ 3JECVUUPOΛόΠϯυ pOE7JFX#Z*EෆཁͰ ී௨ʹ࢖͑ΔΑ͏ʹͳͬͯΔ
  18. ࢖༻ྫ import kotlinx.android.synthetic.activity_main.button ! public class MainActivity: Activity() { override

    fun onCreate(savedInstaceState: Bundle?) { super.onCreate(savedInstaceState) setContentView(R.layout.activity_main) button.setOnClickListener {...}