Python 30์ผ๐Ÿ‘จโ€๐Ÿ’ป - 9์ผ์ฐจ. - OOP ๋ฒ„ํŒ€๋ชฉ.

16251 ๋‹จ์–ด pythonchallenge
๋‚ด๊ฐ€ ํ•™๊ต์—์„œ ๊ฐ€๋ฅด์นœ ์ฒซ ๋ฒˆ์งธ ์–ธ์–ด๋Š” BlueJ ๋‚ด๊ฐ€ 5ํ•™๋…„ ๋•Œ์˜€๋‹ค.์ด๊ฒƒ์€ ์ดˆ๋ณด์ž๋ฅผ ์œ„ํ•œ ์ž๋ฐ” ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์ด์ž ์šฐ๋ฆฌ ํ•™๊ต์˜ ๊ณผ์ •์ด๋‹ค.์ด๊ฒƒ์€ ๋‚ด๊ฐ€ ์ฒ˜์Œ์œผ๋กœ ๋Œ€์ƒ์„ ๋Œ€์ƒ์œผ๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•˜๋Š” ๊ฒƒ์„ ์†Œ๊ฐœํ•œ ๊ฒƒ์ด๋‹ค.๋น„๋ก ๊ทธ ๋™์•ˆ ๋‚˜๋Š” ๊ทธ๋‹ค์ง€ ์ž˜ ์•Œ์ง€ ๋ชปํ–ˆ์ง€๋งŒ, ๋‚˜๋Š” ์—ฌ์ „ํžˆ ์šฐ๋ฆฌ ์ปดํ“จํ„ฐ ์„ ์ƒ๋‹˜์ด OOP์˜ ๊ธฐ๋‘ฅ์„ ์„ค๋ช…ํ•˜๋Š” ๊ฒƒ์„ ๋˜‘๋˜‘ํžˆ ๊ธฐ์–ตํ•œ๋‹ค.์˜ค๋Š˜ ํŒŒ์ดํ†ค์—์„œ OOP์˜ ์›๋ฆฌ๋ฅผ ํƒ์ƒ‰ํ•  ๋•Œ ๋‚˜๋Š” ์„ ์ƒ๋‹˜์˜ ๋ช‡ ๊ฐ€์ง€ ์œ ํ˜•์„ ํšŒ์ƒํ•˜๊ณ  ๊ทธ๊ฒƒ๋“ค์„ ๋‚˜์˜ ๋ช‡ ๊ฐ€์ง€ ์œ ํ˜•๊ณผ ๊ฒฐํ•ฉ์‹œํ‚ค๋ ค๊ณ  ํ–ˆ๋‹ค.
OOP์€ ๊ธฐ๋ณธ์ ์œผ๋กœ 4๊ฐœ์˜ ๊ธฐ๋‘ฅ์ด๋‚˜ ํ•ต์‹ฌ ์›์น™, ์ฆ‰ ์ด๋ฅธ๋ฐ” OOP ๊ธฐ๋‘ฅ์ด ์žˆ๋‹ค

  • ๋ด‰์ธ - ๋น„๋ก ๋‚˜๋Š” ์ง€๋‚œ ๊ธ€์—์„œ OOP์˜ ๊ธฐ์ดˆ ์ง€์‹์„ ์†Œ๊ฐœํ–ˆ์ง€๋งŒ, ๋‚˜๋Š” ์ด๋ฏธ ๋ด‰์ธ์„ ๊ฐ„์ ‘์ ์œผ๋กœ ์†Œ๊ฐœํ–ˆ๋‹ค.๊ฐ„๋‹จํ•˜๊ฒŒ ๋งํ•˜์ž๋ฉด, ๋ด‰์ธ์€ ์šฉ๊ธฐ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜๋ฉฐ, ์ด ์†์„ฑ๊ณผ ๊ด€๋ จ๋œ ์†์„ฑ๊ณผ ์ž‘์—…์ด ํ•จ๊ป˜ ๊ทธ๋ฃนํ™”๋˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.
  • class Avenger:
      def __init__(self, name, knownAs):
        self.name = name
        self.knownAs = knownAs
    
      def reveal_identity(self):
        print(f'I am {self.name}, also known as {self.knownAs}')
    
    hulk = Avenger('Bruce Banner', 'Hulk')
    iron_man = Avenger('Tony Stark', 'IronMan')
    
    hulk.reveal_identity() # I am Bruce Banner, also known as Hulk
    iron_man.reveal_identity() # I am Tony Stark, also known as IronMan
    
    ์ƒ๊ธฐ ํด๋ž˜์Šค์—์„œ reveal_identity ๋ฐฉ๋ฒ•์„ ํ˜ธ์ถœํ•˜๋ฉด ๋ชจ๋“  ๋ณต์ˆ˜์ž์˜ ์‹ ๋ถ„์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.๋‹ค์‹œ ๋งํ•˜๋ฉด ๊ทธ๋“ค์˜ ์‹ ๋ถ„์€ ์ด๋ฏธ ๋ด‰์ธ๋˜์—ˆ๋‹ค.์ด๋Ÿฐ ์–ด๋–ค ์กฐ์ž‘์„ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ข…๋ฅ˜๋ฅผ ์ด๋ ‡๊ฒŒ ๊ฐ•ํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค.ํด๋ž˜์Šค์—์„œ ๋Œ€์ƒ์„ ๋งŒ๋“ค ๋•Œ, ์šฐ๋ฆฌ๋Š” ์ด ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด์„œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ทธ๋“ค์ด ์–ด๋–ป๊ฒŒ ์ด ์ž‘์—…์„ ์‹คํ–‰ํ•˜๋Š”์ง€ ๊ฑฑ์ •ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.๋ณต์ˆ˜์ž์ฒ˜๋Ÿผ!

  • ์ถ”์ƒ์ ์ธ ๊ฐœ๋… - ์šด์ „์„ ๋ฐฐ์šฐ๊ธฐ ์œ„ํ•ด ์šฐ๋ฆฌ๋Š” ๋‹จ์ง€ ์„ธ ๊ฐ€์ง€ ์ธก๋ฉด์˜ ์ง€์‹์ธ a, B์™€ C, ์ฆ‰ ๊ฐ€์†๊ธฐ, ์ œ๋™๊ธฐ์™€ ํด๋Ÿฌ์น˜(์ง€๊ธˆ์€ ์ž๋™์ฐจ๊ฐ€ ์žˆ์œผ๋‹ˆ ๋”์šฑ ๊ฐ„๋‹จํ•˜๋‹ค)๋ฅผ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค.์šด์ „์‚ฌ๋กœ์„œ ์šฐ๋ฆฌ๋Š” ๋ฌด์—‡์ด ์ž๋™์ฐจ๋ฅผ ๊ฐ€์†์‹œํ‚ค๋Š”์ง€ ์•Œ ํ•„์š”๋„, ๋ธŒ๋ ˆ์ดํฌ๋ฅผ ๋ฐŸ์„ ๋•Œ ์ž๋™์ฐจ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ฉˆ์ถ”๋Š”์ง€ ์•Œ ํ•„์š”๋„ ์—†๋‹ค.์ด๊ฒƒ์€ ์ถ”์ƒ์ด๋ผ๊ณ  ํ•œ๋‹ค.๊ทธ๊ฒƒ์€ ํ˜„์‹ค ์ƒํ™œ์—์„œ ์–ด๋””์—๋‚˜ ์žˆ๋‹ค.์ „์› ๋ฒ„ํŠผ์„ ๊ฐ€๋ณ๊ฒŒ ๋ˆŒ๋Ÿฌ ์ปดํ“จํ„ฐ๋ฅผ ๊ฐ€๋™ํ•˜๊ณ  ํ™”๋ฉด์„ ๊ฐ€๋ณ๊ฒŒ ๋‘๋“œ๋ ค ํœด๋Œ€์ „ํ™”์—์„œ ์‚ฌ์ง„์„ ์บก์ฒ˜ํ•œ๋‹ค. ์œ„์˜ ์˜ˆ์—์„œ reveal_identity ๋ฐฉ๋ฒ•์€ ์ถ”์ƒ์ ์ด๋‹ค.
  • class Avenger:
      def __init__(self, name, knownAs):
        self.name = name
        self.knownAs = knownAs
    
      def reveal_identity(self):
        print(f'I am {self.name}, also known as {self.knownAs}')
    
    hulk = Avenger('Bruce Banner', 'Hulk')
    
    hulk.name = 'Thanos'
    hulk.knownAs = 'Loki'
    
    hulk.reveal_identity() # I am Thanos, also known as Loki
    
    ์œ„์˜ ์œ ํ˜• ์‹คํ˜„์— ๋ฌธ์ œ๊ฐ€ ์กด์žฌํ•œ๋‹ค.ํ—ํฌ๊ฐ€ ๋งŒ๋“ค์–ด์กŒ์ง€๋งŒ ๋กœํ‚ค๊ฐ€ ์™”๊ณ  ํ—ํฌ๋ฅผ ํƒ€๋…ธ์Šค๋กœ ์œ„์žฅํ•˜๊ธฐ๋กœ ํ–ˆ์–ด์š”!์ด๊ฒƒ์€ ์ง€๊ธˆ ์›ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. ๋น„๋ก ๋ด‰์ธ๊ณผ ์ถ”์ƒ์„ ์‹คํ˜„ํ–ˆ์ง€๋งŒ ๋‚ด๋ถ€ ๊ธฐ๋Šฅ์€ ์™ธ๋ถ€์—์„œ ์™œ๊ณกํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
    ๊ธฐ๋ณธ์ ์œผ๋กœ Python์€ ๊ฐœ์ธ ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์„ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.๋ณ€์ˆ˜์™€ ๋ฐฉ๋ฒ•์„ ์ ‘๊ทผํ•  ์ˆ˜ ์—†๊ฒŒ ํ•˜๊ณ  ์ง„์ •ํ•œ ์ถ”์ƒ์„ ์–ป์„ ์ˆ˜ ์žˆ๋Š”์ง€ ๋” ๋งŽ์€ ํƒ์ƒ‰์„ ํ•ด์•ผ ํ•  ๊ฒƒ์ด๋‹ค.
    ๊ทธ๋Ÿฌ๋‚˜ ๊ฐœ์ธ ๋ณ€์ˆ˜ ์•ž์— _ ๋ฅผ ๋ถ™์—ฌ์„œ ๊ทธ๊ฒƒ์„ ๋งŒ๋“œ๋Š” ๊ด€๋ก€๊ฐ€ ์žˆ๋‹ค.๋‹ค๋ฅธ ๊ฐœ๋ฐœ์ž๋“ค์ด ์˜์™ธ๋กœ ์ด ๋ณ€์ˆ˜์™€ ๋ฐฉ๋ฒ•์„ ์—…๋ฐ์ดํŠธํ•˜๋ ค๊ณ  ์‹œ๋„ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์ด๋ฅผ ์‚ฌ์œ  ๋ณ€์ˆ˜๋กœ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.์ด๋Š” JavaScript World์—์„œ ๊ฐœ์ธ ๋ณ€์ˆ˜๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐฉ์‹๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

  • ์ƒ์†-๋ง ๊ทธ๋Œ€๋กœ ์ƒ์†์€ ๋ถ€๋Œ€๋กœ๋ถ€ํ„ฐ ์†์„ฑ๊ณผ ๊ธฐ๋Šฅ์„ ๊ณ„์Šนํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.๋ถ€๋ชจ ํด๋ž˜์Šค๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๋ถ€๋ชจ ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๊ณ„์Šน๋œ ๋ชจ๋“  ํ•˜์œ„ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.์ด๋ ‡๊ฒŒ ํ•˜๋Š” ๊ฒƒ์€ ๋น„์Šทํ•œ ํด๋ž˜์Šค ์‚ฌ์ด์—์„œ ๊ธฐ๋Šฅ์„ ๊ณต์œ ํ•˜๊ธฐ ์œ„ํ•ด์„œ์ด๋ฉฐ ๊ฐ™์€ ๋…ผ๋ฆฌ๋ฅผ ๋ฐ˜๋ณตํ•ด์„œ ์ž‘์„ฑํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.
  • class Player:
      def __init__(self, name, age):
        self.name = name
        self.age = age
    
      def run(self):
        return f'{self.name} is running'
    
    class Cricketer(Player): # Syntax to inherit a class
      def catch_ball(self):
        return f'{self.name} Caught the ball'
    
    class Batsman(Cricketer):
      def swing_bat(self):
        return f'what a shot by {self.name}'
    
    player1 = Batsman('Virat Kohli', 31)
    
    print(player1.run())
    print(player1.catch_ball())
    print(player1.swing_bat())
    
    player1์€ ํƒ€์ž๋ฅ˜์˜ ์‹ค๋ก€์ด๋‹ค.ํƒ€์ž๋Š” ํฌ๋ฆฌ์ผ“ ์„ ์ˆ˜์˜ ํ•œ ๋ถ€๋ฅ˜๋‹ค.JavaScript ์„ธ๊ณ„์—์„œ๋Š” ํด๋ž˜์Šค๊ฐ€ extends ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์†๋ฉ๋‹ˆ๋‹ค.๊ทธ๋ž˜์„œ ๋‚˜๋Š” ๋‹จ์ง€ ๋‚˜์˜ ์‹ฌ์ง€ ๋ชจ๋ธ์—์„œ ๋‘˜์„ ๋‹ค๋ฅธ ๋น„๊ต๋ฅผ ํ–ˆ์„ ๋ฟ์ด๋‹ค.ํฌ๋ฆฌ์ผ“๋ฅ˜๋Š” ๋˜ ์„ ์ˆ˜์˜ ํ•˜์œ„ ํด๋ž˜์Šค๋‚˜ ํ•˜์œ„ ํด๋ž˜์Šค์ธ๋ฐ, ๋‹ค์‹œ ๋งํ•˜๋ฉด ์„ ์ˆ˜๋ฅ˜๋ฅผ ๊ณ„์Šนํ•œ ๊ฒƒ์ด๋‹ค.
    ํ”Œ๋ ˆ์ด์–ด1์€ ํƒ€์ž๊ฐ€ ๋งŒ๋“  ๋Œ€์ƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฉ๋ง์ด๋ฅผ ํœ˜๋‘๋ฅด๋Š” ๋™์ž‘์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
    ํฌ๋ฆฌ์ผ“ ์„ ์ˆ˜์˜ ๋ฆฌ์‹œ๋ธŒ ๋Šฅ๋ ฅ๋„ ์ด์–ด๋ฐ›์•˜๋‹ค.๋ฉ‹์žˆ์–ด!
    ๊ทธ๊ฒƒ์€ ๋˜ํ•œ ์œ ์ €๋ฅ˜์˜ ๋‹ฌ๋ฆฌ๊ธฐ ๋Šฅ๋ ฅ์„ ๊ณ„์Šนํ–ˆ๋‹ค.
    Python์—๋Š” ๋Œ€์ƒ์ด ํด๋ž˜์Šค์˜ ์‹ค๋ก€์ธ์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ์•„์ฃผ ํŽธ๋ฆฌํ•œ ๋‚ด์žฅ ํ•จ์ˆ˜ isinstance ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
    class Player:
      def __init__(self, name, age):
        self.name = name
        self.age = age
    
      def run(self):
        return f'{self.name} is running'
    
    class Cricketer(Player):
      def catch_ball(self):
        return f'{self.name} Caught the ball'
    
    class Batsman(Cricketer):
      def swing_bat(self):
        return f'what a shot by {self.name}'
    
    player1 = Batsman('Virat Kohli', 31)
    
    print(isinstance(player1, Batsman)) # True
    print(isinstance(player1, Cricketer)) # True
    print(isinstance(player1, Player)) # True
    print(isinstance(player1, object)) # True
    

    Since everything in Python is an object, all classes inherit all properties and methods from this base class object


  • ๋‹ค์ค‘์„ฑ-ํด๋ฆฌ๋Š” ๋งŽ์Œ์„ ๋‚˜ํƒ€๋‚ด๊ณ  morphism์€ ํ˜•์‹์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.๊ทธ๋ž˜์„œ ๊ทธ ๊ธ€์ž์˜ ๋œป์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํ˜•์‹์œผ๋กœ ๋‚˜ํƒ€๋‚œ๋‹ค.๋ชจ๋“  ์š”์†Œ์˜ ์š”์†Œ ๋Œ€์ƒ๋ฅ˜๋Š” ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•์€ ๋Œ€์ƒ์ด ํ˜ธ์ถœํ•œ ๋Œ€์ƒ์— ๋”ฐ๋ผ ์„œ๋กœ ๋‹ค๋ฅธ ์กฐ์ž‘์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • class ProgrammingLanguage:
      def __init__(self, name):
        self.name = name
    
    class JavaScript(ProgrammingLanguage):
      def comment(self):
        return(f'// A Comment in {self.name}')
    
    class Python(ProgrammingLanguage):
      def comment(self):
        return(f'# A comment in {self.name}')
    
    language1 = JavaScript('JavaScript')
    language2 = Python('Python')
    
    def add_comment(languageObject):
      print(languageObject.comment())
    
    add_comment(language1) # // A Comment in JavaScript
    add_comment(language2) # # A comment in Python
    
    for language in [language1, language2]:
      print(language.comment())
    # // A Comment in JavaScript
    # # A comment in Python
    
    ์œ„์˜ ์ฝ”๋“œ ๋ธ”๋ก์—์„œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์˜ ๋Œ€์ƒ์„ ๋ฐ›์•„๋“ค์ด๊ณ comment () ๋ฐฉ๋ฒ•์„ ํ˜ธ์ถœํ•˜๋Š” ์œ ๋‹ˆ๋ฒ„์„ค ํ•จ์ˆ˜add comment๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.๊ฐ™์€comment ๋ฐฉ๋ฒ•์€ ์„œ๋กœ ๋‹ค๋ฅธ ๋Œ€์ƒ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ˜ธ์ถœํ•  ๋•Œ ๊ทธ๊ฒƒ๋“ค์ดcomment ๋ฐฉ๋ฒ•์˜ ์‹คํ˜„์— ๋”ฐ๋ผ ์„œ๋กœ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ๋‚ณ๋Š”๋‹ค.
    ๋‘ ๋ฒˆ์งธ ๊ฒฝ์šฐ ์–ธ์–ด ๋Œ€์ƒ ๋ชฉ๋ก์„ ์ˆœํ™˜ํ•  ๋•Œ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ๋•Œ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค.
    ์ด๊ฒƒ์€ ๋‹คํƒœ์„ฑ์ด๋‹ค.
    OOP ์›์น™์„ ์–ธ์ œ ์‚ฌ์šฉํ• ์ง€ ๊ตฌ์ฒด์ ์ธ ์ง€์นจ์ด๋‚˜ ๊ทœ์น™์€ ์—†์ง€๋งŒ ๋ฌธ์ œ ์ง„์ˆ ์— ๋‹ฌ๋ ค ์žˆ๊ณ  ๋‹ค์–‘ํ•œ ๋ฐฉ์‹์œผ๋กœ OOP์˜ ๊ฐœ๋…์„ ์‘์šฉํ•ด ํ•ด๊ฒฐ ๋ฐฉ์•ˆ์„ ์‹คํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.
    ์˜ค๋Š˜์€ ์—ฌ๊ธฐ๊นŒ์ง€.๋‚˜์˜ ์‹ฌ์ง€ ๋ชจ๋ธ์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ฐœ๋…์˜ ์ˆ˜์ •๊ฒฉ์„ ๊ตฌ์ถ•ํ•˜๊ธฐ ์œ„ํ•ด ์ ์ฐจ ์ง„ํ™”ํ•˜๊ณ  ์žˆ์„์ง€๋„ ๋ชจ๋ฅธ๋‹ค.
    ๋‚ด์ผ ๋‚˜๋Š” ๋‚จ์€ OOP ๊ฐœ๋…์„ ์™„์„ฑํ•˜๊ณ  ์žฌ๋ฏธ์žˆ๋Š” ์ธ์ฝ”๋”ฉ ์—ฐ์Šต์„ ํ•œ ๋‹ค์Œ์— ๋˜ ๋‹ค๋ฅธ ํฅ๋ฏธ๋กœ์šด ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฒ”๋ก€๋ฅผ ์‹œ์ž‘ํ•  ๊ณ„ํš์ด๋‹ค.
    ์ฆ๊ฑฐ์šด ์‹œ๊ฐ„ ๋ณด๋‚ด์„ธ์š”!

    ์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ