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

SwiftにおけるMethod Dispatchについて

SwiftにおけるMethod Dispatchについて

SwiftのMethod Dispatchについて色々とまとめてみました。

Naohiro Hamada

February 14, 2017
Tweet

More Decks by Naohiro Hamada

Other Decks in Programming

Transcript

  1. ໰୊ protocol AlcoholProtocol { } extension AlcoholProtocol { func alcohol()

    -> String { return "" } } class TwoBeers { } extension TwoBeers: AlcoholProtocol { func alcohol() -> String { return "" } } let beerServer: AlcoholProtocol = TwoBeers() let myBeer = beerServer.alcohol() print(myBeer) 
  2. ໰୊ protocol AlcoholProtocol { } extension AlcoholProtocol { func alcohol()

    -> String { return "" } } class TwoBeers { } extension TwoBeers: AlcoholProtocol { func alcohol() -> String { return "" } } let beerServer: AlcoholProtocol = TwoBeers() let myBeer = beerServer.alcohol() print(myBeer) print(myBeer)Ͱग़ྗ͞ΕΔͷ͸ ͱͷͲͬͪʁ 
  3. .FUIPE%JTQBUDIͱ͸ʁ w ιʔείʔυ্ͷʮϝιουݺͼग़͠ʯͰɺ࣮ࡍʹ ݺͼग़͞ΕΔϝιου͸ɺͲͷϝιουͳͷ͔ʁɹ ΛܾΊΔػߏ extension AlcoholProtocol { func alcohol()

    -> String { return "" } } class TwoBeers { } extension TwoBeers: AlcoholProtocol { func alcohol() -> String { return "" } } let beerServer: AlcoholProtocol = TwoBeers() let myBeer = beerServer.alcohol() ʁ 
  4. 4UBUJD%JTQBUDI w ίϯύΠϧ࣌ʹɺͲͷϝιο υΛݺͼग़͔͢ɺ͕ܾ·Δ TUSVDU#FFS4FSWFS\ GVODCFFS 4USJOH\ SFUVSO ^ GVODCFFST

    4USJOH\ SFUVSO ^ ^ DMBTT#FFS4FSWFS\ pOBMGVODCFFS 4USJOH\ SFUVSO ^ ^ 
  5. 75BCMF w Ծ૝ςʔϒϧΛ࢖ͬͯɺ ࣮ߦ࣌ʹݺͼग़͢ϝιο υΛܾΊΔ w Ϋϥε͸ɺԾ૝ςʔϒ ϧΛ࣋ͭ class AlcoholServer

    { func alcohol() -> String { return "" } func cheers() -> String { return "" } } class SakeMaker: AlcoholServer { override func alcohol() -> String { return "" } } 
  6. 75BCMF class AlcoholServer { func alcohol() -> String { return

    "" } func cheers() -> String { return "" } } class SakeMaker: AlcoholServer { override func alcohol() -> String { return "" } } "MDPIPM4FSWFS BMDPIPM "MDPIPM4FSWFS DIFFST "MDPIPM4FSWFS 4BLF.BLFS BMDPIPM 4BLF.BLFS DIFFST "MDPIPM4FSWFS 
  7. .FTTBHF w .FTTBHFΛ࢖ͬͯɺͲͷ ϝιουΛݺͼग़͔͢Λ ܾΊΔ w 0CKFDUJWF$Ͱͷϝιου ݺͼग़͠ w 'PVOEBUJPOΛΠϯϙʔ

    τ͢Δඞཁ͕͋Δ JNQPSU'PVOEBUJPO DMBTT#FFS4FSWFS\ EZOBNJDGVODBMDPIPM 4USJOH\ SFUVSO ^ EZOBNJDGVODDIFFST 4USJOH\ SFUVSO ^ ^ 
  8. 8JUOFTT w ϓϩτίϧʹ४ڌ͍ͯ͠Δ ৔߹ʹ࡞ΒΕΔ w ϓϩτίϧͰఆٛ͞Εͨϝ ιουͷݺͼग़͠Λ͢Δͱ ͖ʹ࢖ΘΕΔ w σϑΥϧτ࣮૷༻ͷ

    8JUOFTT΋͋Δ QSPUPDPM#FFS\ GVODCFFS 4USJOH ^ QSPUPDPM4BLF\ GVODTBLF 4USJOH ^ TUSVDU"MDPIPM4FSWFS#FFS 4BLF\ GVODCFFS 4USJOH\ SFUVSO ^ GVODTBLF 4USJOH\ SFUVSO ^ ^ 
  9. ໰୊ protocol AlcoholProtocol { } extension AlcoholProtocol { func alcohol()

    -> String { return "" } } class TwoBeers { } extension TwoBeers: AlcoholProtocol { func alcohol() -> String { return "" } } let beerServer: AlcoholProtocol = TwoBeers() let myBeer = beerServer.alcohol() print(myBeer) print(myBeer)Ͱग़ྗ͞ΕΔͷ͸ ͱͷͲͬͪʁ
  10. ໰୊ protocol AlcoholProtocol { } extension AlcoholProtocol { func alcohol()

    -> String { return "" } } class TwoBeers { } extension TwoBeers: AlcoholProtocol { func alcohol() -> String { return "" } } let beerServer: AlcoholProtocol = TwoBeers() let myBeer = beerServer.alcohol() print(myBeer)