Androidアプリを作る 第3回

9b6ca173883528d3bd047ca8f20b0bce?s=47 nonylene
November 06, 2016

Androidアプリを作る 第3回

9b6ca173883528d3bd047ca8f20b0bce?s=128

nonylene

November 06, 2016
Tweet

Transcript

  1. "OESPJEΞϓϦΛ࡞Δ  ,.$OPOZMFOF +BWBޙ൒

  2. ࣗݾ঺հ w OPOZMFOF ͷʹΕΜ  w ,.$ճ w SPPU

  3. ࣗݾ঺հ w OPOZMFOF ͷʹΕΜ  w εϚϗΞϓϦͱ͔ w 5XJUUFS(JU)VCFUD w

    IUUQOPOZMFOFIBUFOBCMPHKQ
  4. ͜Μ͹Μ͸

  5. ࠓ೔΍Δ͜ͱ  +BWBޙ൒  Ϋϥεܧঝ  ܕม׵ʢΩϟετʣ  ͦͷଞ+BWBͷจ๏

  6. +BWBޙ൒

  7. Ϋϥεܧঝ

  8. Ϋϥεܧঝͱ͸ w ͋ΔΫϥεΛݩʹͯ͠৽͍͠ΫϥεΛ࣮૷ w ॲཧͷڞ௨ԽɾΫϥεͷ֊૚Խ

  9. Ϋϥεܧঝͱ͸ w +BWBͰ͸ҰͭͷΫϥε͔Β͔͠ܧঝͰ͖ͳ͍ w ʢଟॏܧঝ͸Ͱ͖ͳ͍ʣ

  10. ਌Ϋϥε ࢠΫϥε ܧঝ ʢεʔύʔΫϥεʣ ʢαϒΫϥεʣ

  11. ࣮ྫ

  12. ·Μ͕λΠϜ͖ΒΒ
 ͰֶͿΫϥεܧঝ

  13. ·Μ͕λΠϜ͖ΒΒͱ͸ w ๕จࣾͷίϚϚϯΨࢽͦͷ࢞ຓࢽ܈ w ·Μ͕λΠϜͷएऀ޲͚ͱ͍͏Ґஔ͚ͮ

  14. ·Μ͕λΠϜ͖ΒΒୡ w ·Μ͕λΠϜ͖ΒΒ w ·Μ͕λΠϜ͖ΒΒΩϟϥοτ w ·Μ͕λΠϜ͖ΒΒ."9 w ·Μ͕λΠϜ͖ΒΒϑΥϫʔυ w

    ·Μ͕λΠϜ͖ΒΒϛϥΫ
  15. ·Μ͕λΠϜ͖ΒΒ ·Μ͕λΠϜ ͖ΒΒ ·Μ͕λΠϜ ͖ΒΒ Ωϟϥοτ ·Μ͕λΠϜ ͖ΒΒ ."9 ·Μ͕λΠϜ

    ͖ΒΒ ϑΥϫʔυ ·Μ͕λΠϜ ͖ΒΒ ϛϥΫ
  16. ίϛοΫࡶࢽ ·Μ͕λΠϜ͖ΒΒ ·Μ͕λΠϜ ͖ΒΒ ·Μ͕λΠϜ ͖ΒΒ Ωϟϥοτ ·Μ͕λΠϜ ͖ΒΒ ."9

    ·Μ͕λΠϜ ͖ΒΒ ϑΥϫʔυ ·Μ͕λΠϜ ͖ΒΒ ϛϥΫ
  17. ίϛοΫࡶࢽ ࡶࢽ ·Μ͕λΠϜ͖ΒΒ ·Μ͕λΠϜ ͖ΒΒ ·Μ͕λΠϜ ͖ΒΒ Ωϟϥοτ ·Μ͕λΠϜ ͖ΒΒ

    ."9 ·Μ͕λΠϜ ͖ΒΒ ϑΥϫʔυ ·Μ͕λΠϜ ͖ΒΒ ϛϥΫ
  18. ComicMagazine Magazine BaseMangaTimeKirara MangaTimeKirara, MangaTimeKiraraCarat, MangaTimeKiraraMax, etc…

  19. Magazine BaseMangaTimeKirara MangaTimeKirara, MangaTimeKiraraCarat, MangaTimeKiraraMax, etc… ( MangaTimeKirara ͕͋ΔͨΊ )

    ComicMagazine
  20. .BHB[JOFΫϥε࣮૷ w λΠτϧɾδϟϯϧɾձࣾɾ߸਺Λอ࣋ w આ໌༻จষ΋ग़ྗ͢Δ

  21. .BHB[JOFΫϥε࣮૷ * Magazine.java class Magazine { private String title; private

    String genre; private String company; private int number; public Magazine(String title, String genre, String company,
 int number) { this.title = title; this.genre = genre; this.company = company; this.number = number; } public String getInfoText() { return company + " ͷ " + title + number + "߸"; } }
  22. $PNJD.BHB[JOF࣮૷ w δϟϯϧ͸ອըͩͱ෼͔͍ͬͯΔ ˠ δϟϯϧͷҾ਺͸ෆཁ
 w ·ͣ͸ී௨ʹ࣮૷

  23. $PNJD.BHB[JOF࣮૷ * ComicMagazine.java class ComicMagazine { private String title; private

    String genre = "ອըࡶࢽ"; private String company; private int number; public ComicMagazine(String title, String company, int number) { this.title = title; this.company = company; this.number = number; } public String getInfoText() { return company + "ͷ" + title + number + "݄߸"; } }
  24. $PNJD.BHB[JOF࣮૷ ComicMagazine magazine = new ComicMagazine("·Μ͕λΠϜ͖ΒΒMAX", "๕จࣾ", 1); String text

    = magazine.getInfoText();
  25. .BHB[JOFܧঝ w MagazineΛܧঝ͢ΔΑ͏ʹมߋ͢Δ • extends Magazine

  26. .BHB[JOFܧঝ w Magazine͔Βϝϯόม਺ϝιουFUDʜ ͕Ҿ͖ܧ͕ΕΔ w ίϯετϥΫλ͸Ҿ͖ܧ͕Εͳ͍ w ࢠΫϥε಺ͰݺͿඞཁ͕͋Δ Magazine ComicMagazine

    titile / author … getInfoText() ਌ ࢠ
  27. .BHB[JOFܧঝ w Magazine͔Βϝϯόม਺ϝιουFUDʜ ͕Ҿ͖ܧ͕ΕΔ w ࢠΫϥε಺෦͔Βݟ͑Δ͔Ͳ͏͔͸ผ w privateͳ΋ͷ͸ݟ͑ͳ͍ Magazine ComicMagazine

    titile / author … getInfoText() ਌ ࢠ
  28. .BHB[JOFܧঝ * CodeMagazine.java class ComicMagazine extends Magazine { public ComicMagazine(String

    title, String company, int number) { super(title, "ອըࡶࢽ", company, number); } } Magazine Λܧঝ
  29. .BHB[JOFܧঝ * CodeMagazine.java class ComicMagazine extends Magazine { public ComicMagazine(String

    title, String company, int number) { super(title, "ອըࡶࢽ", company, number); } } Magazine ʹఆٛ͞ΕͨίϯετϥΫλΛݺͿ ※ genre Λৗʹ “ອըࡶࢽ” ͱ͍ͯ͠Δ
  30. .BHB[JOFܧঝ ComicMagazine magazine = new ComicMagazine("·Μ͕λΠϜ͖ΒΒMAX", "๕จࣾ", 1); String text

    = magazine.getInfoText(); Magazine ͷϝιου͕࢖͑Δ
  31. #BTF.BOHB5JNF,JSBSB࣮૷ w ͖ΒΒୡڞ௨ͷ৽͍͠ػೳΛ࡞Γ͍ͨ w දࢴΛ০Δ࡞඼Λ؅ཧ͍ͨ͠
 ˠ ৽͍͠ม਺ coverTitle ͕ඞཁ ˠ

    ৽͍͠Ϋϥε BaseMangaTimeKirara
  32. * BaseMangaTimeKirara.java class BaseMangaTimeKirara extends ComicMagazine { private String coverTitle;

    public BaseMangaTimeKirara(String title, String coverTitle, int number) { super(title, "๕จࣾ", number); this.coverTitle = coverTitle; } public String getCoverTitle() { return coverTitle; } } #BTF.BOHB5JNF,JSBSB࣮૷ coverTitle ม਺Λఆٛ
  33. * BaseMangaTimeKirara.java class BaseMangaTimeKirara extends ComicMagazine { private String coverTitle;

    public BaseMangaTimeKirara(String title, String coverTitle, int number) { super(title, "๕จࣾ", number); this.coverTitle = coverTitle; } public String getCoverTitle() { return coverTitle; } } #BTF.BOHB5JNF,JSBSB࣮૷ ComicMagazine ΫϥεͷίϯετϥΫλΛݺͿ
  34. * BaseMangaTimeKirara.java class BaseMangaTimeKirara extends ComicMagazine { private String coverTitle;

    public BaseMangaTimeKirara(String title, String coverTitle, int number) { super(title, "๕จࣾ", number); this.coverTitle = coverTitle; } public String getCoverTitle() { return coverTitle; } } #BTF.BOHB5JNF,JSBSB࣮૷ coverTitle ʹ୅ೖ͢Δ ※ ComicMagazine ʹ͸ coverTitle ͸ͳ͍
  35. * BaseMangaTimeKirara.java class BaseMangaTimeKirara extends ComicMagazine { private String coverTitle;

    public BaseMangaTimeKirara(String title, String coverTitle, int number) { super(title, "๕จࣾ", number); this.coverTitle = coverTitle; } public String getCoverTitle() { return coverTitle; } } #BTF.BOHB5JNF,JSBSB࣮૷ getCoverTitle() ϝιουΛఆٛ ※ ComicMagazine ʹ͸ getCoverTitle() ͸ͳ͍
  36. #BTF.BOHB5JNF,JSBSB׬੒ BaseMangaTimeKirara kirara = new BaseMangaTimeKirara(“·Μ͕λΠϜ͖ΒΒMAX”, "͖Μ͍ΖϞβΠΫ", 1); String cover

    = kirara.getCoverTitle(); String text = kirara.getInfoText();
  37. .BOHB5JNF,JSBSB."9࣮૷ w ͖ΒΒ."9Ͱ͸આ໌จΛม͍͑ͨ w ࠓʮ๕จࣾͷ·Μ͕λΠϜ͖ΒΒ."9݄߸ʯ w ."9
 ʮ·Μ͕λΠϜ͖ΒΒͷυΩυΩϏδϡΞϧίϛο Ϋεʂࠓ݄ͷ·Μ͕λΠϜ͖ΒΒ."9݄߸ͷද ࢴ͸͖Μ͍ΖϞβΠΫʂʯ

  38. .BOHB5JNF,JSBSB."9࣮૷ w getInfoText()ͷ಺༰Λมߋ͢Δඞཁ
 ˠ ΦʔόʔϥΠυ 0WFSSJEF Λߦ͏

  39. w લ४උ w MangaTimeKiraraMAX͔Β
 title / number / coverTitle ʹ


    ΞΫηεͰ͖ΔΑ͏ʹ͢Δ .BOHB5JNF,JSBSB."9࣮૷
  40. w લ४උ w ࠓճ͸ϝϯόม਺ͷΞΫηεम০ࢠΛมߋ w ϝιουΛ࡞੒͢Δ৔߹΋͋Δ w private ࣗ෼ͷΫϥε಺ͷΈ 


    protected ࣗ෼ͱࢠΫϥε಺ͷΈ .BOHB5JNF,JSBSB."9࣮૷
  41. ΞΫηεम০ࢠมߋ * Magazine.java class Magazine { protected String title; private

    String genre; private String company; protected int number; public Magazine(… }
  42. ΞΫηεम০ࢠมߋ * BaseMangaTimeKirara.java class BaseMangaTimeKirara extends ComicMagazine { protected String

    coverTitle; public BaseMangaTime… }
  43. .BOHB5JNF,JSBSB."9࣮૷ w getInfoText() ΛΦʔόʔϥΠυ͢Δ w @OverrideΞϊςʔγϣϯ ޙड़ Λ෇͚Δ w ෇͚ͳͯ͘΋ίϯύΠϧ௨Δέʔε΋͋Δ͚Ͳ


    ෇͚·͠ΐ͏
  44. .BOHB5JNF,JSBSB."9࣮૷ * MangaTimeKiraraMAX.java class MangaTimeKiraraMAX extends BaseMangaTimeKirara { public MangaTimeKiraraMAX(String

    coverTitle, int number) { super("·Μ͕λΠϜ͖ΒΒMAX", coverTitle, number); } @Override public String getInfoText() { return "·Μ͕λΠϜ͖ΒΒͷυΩυΩϏδϡΞϧίϛοΫε!ࠓ݄ͷ" + title + number + "݄߸ͷදࢴ͸" + coverTitle + "!"; } } getInfoText() Λ Override ͢Δ
  45. BaseMangaTimeKirara kirara = new BaseMangaTimeKirara( "·Μ͕λΠϜ͖ΒΒMAX", "͖Μ͍ΖϞβΠΫ" , 1); String

    text = kirara.getInfoText(); MangaTimeKiraraMAX max = new MangaTimeKiraraMAX( "͖Μ͍ΖϞβΠΫ" , 1); String maxText = max.getInfoText(); .BOHB5JNF,JSBSB."9׬੒ UFYU๕จࣾͷ·Μ͕λΠϜ͖ΒΒ."9݄߸ NBY5FYU·Μ͕λΠϜ͖ΒΒͷυΩυΩʢུʣ
  46. Կ͕خ͍͔͠ w ࡶࢽؔ܎ͷॲཧΛߦ͍͍ͨ࣌ʜ w ࡶࢽશମͷॲཧ͸MagazineΛҾ਺ʹऔΔ w ଞͷMagazineͷࢠΫϥε΋࢖͑Δ w ·Μ͕λΠϜ͖ΒΒઐ༻ͷॲཧ͸
 MangaTimeKiraraΛҾ਺ʹऔΔ

  47. ྫ public void printInfo(Magazine magazine) { System.out.println(magazine.getInfoText()); } public void

    printCoverTitle(BaseMangaTimeKirara magazine) { System.out.println(magazine.getCoverTitle()); } MangaTimeKiraraMAX max = new MangaTimeKiraraMAX("͖Μ͍ΖϞβΠΫ" , 1); printInfo(max); printCoverTitle(max);
  48. "CTUSBDU
 Ϋϥε

  49. "CTUSBDUΫϥε w +BQBOFTFந৅Ϋϥε w 㲗۩ମ w ϝιουͷ۩ମతͳॲཧΛࢠΫϥεʹ೚ͤΔ w ϝιουΛ࣮૷ͤͣʹએݴ͚ͩ͢Δ͜ͱ͕Ͱ͖Δ w

    "CTUSBDUͰͳ͍ ࢠΫϥε͸ඞͣϝιουΛ࣮૷
  50. "CTUSBDUΫϥε w ۩ମతͳॲཧΛॻ͔ͳͯ͘΋ྑ͍
 ˠ ܧঝ͞ΕΔ͜ͱ͕લఏͷΫϥε ˠ "CTUSBDUΫϥεࣗମͷΦϒδΣΫτ͸
 ࡞Εͳ͍

  51. ྫ w ·Μ͕λΠϜ͖ΒΒઐ༻ʹɺ
 ΞχϝԽ͍ͯ͠Δອը͕͋Ε͹ฦ͢ػೳ࡞੒ w ͱΓ͋͑ͣࠓճ͸Ϋϥε͝ͱʹݻఆ஋Λฦ͢ w BaseMangaTimeKiraraΛ"CTUSBDUʹ w ͜ͷΫϥε͸ࡶࢽͦͷ΋ͷͰ͸ͳ͍

    w ܧঝ͞ΕΔ͜ͱ͕લఏ
  52. * BaseMangaTimeKirara.java abstract class BaseMangaTimeKirara extends ComicMagazine { protected String

    coverTitle; public BaseMangaTimeKirara(String title, String coverTitle, int number) { super(title, , number); this.coverTitle = coverTitle; } public String getCoverTitle() { return coverTitle; } public abstract String getAnimeTitle(); } abstract म০ࢠΛ͚ͭΔ
  53. * BaseMangaTimeKirara.java abstract class BaseMangaTimeKirara extends ComicMagazine { protected String

    coverTitle; public BaseMangaTimeKirara(String title, String coverTitle, int number) { super(title, , number); this.coverTitle = coverTitle; } public String getCoverTitle() { return coverTitle; } public abstract String getAnimeTitle(); } BCTUSBDUϝιουΛ௥Ճʢ࣮૷͸ॻ͔ͳ͍ʣ
  54. w getAnimeTitle()ΛΦʔόʔϥΠυ͢Δ w ͠ͳ͍ͱίϯύΠϧΤϥʔ .BOHB5JNF,JSBSB."9࣮૷

  55. * MangaTimeKiraraMAX.java class MangaTimeKiraraMAX extends BaseMangaTimeKirara { public MangaTimeKiraraMAX(String coverTitle,

    int number) { super("·Μ͕λΠϜ͖ΒΒMAX", coverTitle, number); } @Override public String getInfoText() { return “·Μ͕λΠϜ͖ΒΒ…" + …(ུ); } @Override public String getAnimeTitle() { return "εςϥͷ·΄͏"; } }
  56. "CTUSBDUΫϥεΛҾ਺ʹ public void printAnimeTitle(BaseMangaTimeKirara magazine) { System.out.println( magazine.getAnimeTitle() ); }

    // BaseMangaTimeKirara ͸΋͏ΦϒδΣΫτΛ௚઀࡞੒Ͱ͖ͳ͍ MangaTimeKiraraMAX max = new MangaTimeKiraraMAX("͖Μ͍ΖϞβΠΫ" , 1); printAnimeTitle(max); ௨ৗͷΫϥεͱಉ͡Α͏ʹϝιουΛݺ΂Δ
  57. *OUFSGBDF

  58. *OUFSGBDF w ΠϯλʔϑΣʔε w ந৅ϝιουͱఆ਺ͷΈ w ܕͱͯ͠࢖͑ΔʢࣅͯΔ͕ΫϥεͰ͸ͳ͍ʣ w ௚઀ΦϒδΣΫτΛ࡞੒Ͱ͖ͳ͍ w

    ΠϯλʔϑΣʔεΛʮ࣮૷ JNQMFNFOU ͢Δʯ ͱ͍͏
  59. *OUFSGBDFͱ
 "CTUSBDU$MBTT w "CTUSBDU$MBTT͸਌ࢠؔ܎ w " ࢠ JT# ਌ 


    w *OUFSGBDF͸ೳྗతͳײ͡ w " Ϋϥε DBO# ΠϯλʔϑΣʔε
  60. *OUFSGBDF w ΠϯλʔϑΣʔε͸ଟॏʹ࣮૷Մೳʢॏཁʣ w ʮ͜ͷΫϥε͸͜͏͍͏ػೳΛ࣋ͪ·͢Αʯͱ͍ ͏ҙຯ߹͍ͳͷͰ

  61. ྫ w ࡶࢽΛϑΝΠϧʹग़ྗ͍ͨ͠ w ʮϑΝΠϧʹग़ྗͰ͖Δ΍ͭʯͱ͍͏*OUFSGBDF Λ࡞Δ • FileExpotable Λ࡞Δ

  62. * FileExportable.java interface FileExportable { public void export(); ʩ 'JMF&YQPSUBCMF࣮૷

    interface Λఆٛ
  63. * FileExportable.java interface FileExportable { void export(); ʩ 'JMF&YQPSUBCMF࣮૷ ϝιουΛఆٛ

    (abstract ෆཁ)
  64. .BHB[JOF࣮૷ w MagazineʹFileExportableΛ࣮૷͢Δ

  65. class Magazine implements FileExportable { protected String tit… public Magazine(String

    titl…) { this.title = ti…; } public String getInfoText() { return co… } @Override public void export() { // export ॲཧΛॻ͘ } } FileExportable Λ implement ͢Δ
  66. class Magazine implements FileExportable { protected String tit… public Magazine(String

    titl…) { this.title = ti…; } public String getInfoText() { return co… } @Override public void export() { // export ॲཧΛॻ͘ } } export ϝιουΛ࣮૷͢Δ
  67. class Magazine implements FileExportable, Hoge, Piyo { } w *OUFSGBDF͸ෳ਺࣮૷Մೳ

    w “,”Ͱ۠੾ͬͯॻ͘ w ͦΕͧΕʹఆٛ͞ΕͨϝιουΛ࣮૷͢Δ
  68. w ܧঝͱ࣮૷͸ಉ࣌ʹߦ͑Δ w ܧঝ΍࣮૷ؒͰϝιου໊͕ඃͬͯ΋0, class BaseMangaTimeKirara extends ComicMagazine implements Foo

    { }
  69. public void exportFile( FileExportable exportable ) { exportable.export(); } MangaTimeKiraraMAX

    max = new MangaTimeKiraraMAX("͖Μ͍ΖϞβΠΫ" , 1); exportFile(max); Ҿ਺ͷܕʹΠϯλʔϑΣʔεΛࢦఆ͢Δ
  70. public void exportFile( FileExportable exportable ) { exportable.export(); } MangaTimeKiraraMAX

    max = new MangaTimeKiraraMAX("͖Μ͍ΖϞβΠΫ" , 1); exportFile(max); FileExportable Λ࣮૷͍ͯ͠ΔΫϥεͰݺͼग़͠
  71. ComicMagazine Magazine BaseMangaTimeKirara MangaTimeKirara, MangaTimeKiraraCarat, MangaTimeKiraraMax, etc… FileExportable Hoge Foo

  72. ܕม׵

  73. ܕม׵ w ϓϩάϥϜͷதͰม਺Λ
 ͲͷΫϥεɾΠϯλʔϑΣʔεͱͯ͠ѻ͏͔ w มߋͯ͠΋ΦϒδΣΫτͷ࣮ମ͸มΘΒͳ͍ ˠ ϝιουɾม਺ͷ݁Ռ͸มΘΒͳ͍
 ʢݺͼग़ͤΔϝιουɾม਺͕มΘΔʣ

  74. ܕม׵ͷछྨ w ҉໧తܕม׵ w ࣗ໌ͳ΋ͷ w উखʹ΍ͬͯ͘ΕΔ w ໌ࣔతܕม׵ʢΩϟετʣ w

    ࣗ໌Ͱͳ͍΋ͷ w ࣗ෼Ͱ໌ࣔ͢Δඞཁ͕͋Δ
  75. ܕม׵ͷྫ w ਌ΫϥεΛҾ਺ʹऔΔϝιουʹࢠΫϥεΛ ೖΕΔ࣌ʹܕม׵͢Δʢ҉໧తʣ
 w ܕม׵͢Δ͜ͱͰ਌Ϋϥεʹͳ͍ࢠΫϥεͷ ϝιουΛݺͼग़͢ʢ໌ࣔతʣ

  76. ComicMagazine Magazine BaseMangaTimeKirara MangaTimeKirara, MangaTimeKiraraCarat, MangaTimeKiraraMax, etc… FileExportable Hoge Foo

  77. ҉໧తͳܕม׵ ComicMagazine Magazine Magazine magazine = new ComicMagazine(…); ਌ ࢠ

  78. ҉໧తͳܕม׵ FileExportable exportable = new Magazine(…); ΠϯλʔϑΣʔε ࣮૷Ϋϥε FileExportable Magazine

  79. ҉໧తͳܕม׵ FileExportable exportable = new MangaTimeKirara(…); ΠϯλʔϑΣʔε ࣮૷ΫϥεͷࢠΫϥεɹ FileExportable MangaTimeKirara

  80. ҉໧తͳܕม׵
 ʢϓϦϛςΟϒܕʣ ਫ਼౓ߴ͍ ਫ਼౓௿͍ long int long longValue = 2;

    ৄࡉ: http://www.atmarkit.co.jp/ait/articles/0803/18/news149.html
  81. ComicMagazine Magazine Magazine magazine = new ComicMagazine(…); ComicMagazine comicMagazine =

    (ComicMagazine) magazine; ਌ ࢠ ໌ࣔతͳܕม׵ʢΩϟετʣ
  82. FileExportable exportable = new Magazine(…); Magazine magazine = (Magazine) magazine;

    ΠϯλʔϑΣʔε ࣮૷Ϋϥε FileExportable Magazine ໌ࣔతͳܕม׵ʢΩϟετʣ
  83. ໌ࣔతͳܕม׵
 ʢΩϟετ ϓϦϛςΟϒܕʣ ਫ਼౓ߴ͍ ਫ਼౓௿͍ long int long longValue =

    2; int intValue = (int) longValue; ৄࡉ: http://www.atmarkit.co.jp/ait/articles/0803/18/news149.html
  84. ଓ͖͸HJTUͰ IUUQTHJTUHJUIVCDPNOPOZMFOF FBGGFGBGDFFBC