Guidelines

객체에 μœ μ—°μ„±μ„ λΆ€μ—¬ν•˜λŠ” λ‹€ν˜•μ„±

λ‹€ν˜•μ„±(Polymorphism)은 객체 지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ˜ 핡심 κ°œλ… 쀑 ν•˜λ‚˜λ‘œ, 'ν•˜λ‚˜μ˜ λ©”μ„œλ“œλ‚˜ ν΄λž˜μŠ€κ°€ λ‹€μ–‘ν•œ λ°©μ‹μœΌλ‘œ μž‘λ™ν•˜λŠ” λŠ₯λ ₯'을 μ˜λ―Έν•©λ‹ˆλ‹€.


λ‹€ν˜•μ„± μ‚¬μš© μ˜ˆμ‹œ

μ•„λž˜λŠ” λ‹€ν˜•μ„±μ„ μ μš©ν•œ 'Animal' ν΄λž˜μŠ€μ™€ κ·Έ μ„œλΈŒν΄λž˜μŠ€ 'Dog' 및 'Cat'의 μ˜ˆμ‹œμž…λ‹ˆλ‹€:

λ‹€ν˜•μ„± μ‚¬μš© μ˜ˆμ‹œ
class Animal: def speak(self): return "μ–΄λ–€ μ†Œλ¦¬λ₯Ό λƒ…λ‹ˆλ‹€." class Dog(Animal): # Animal 클래슀λ₯Ό μƒμ†λ°›μŒ def speak(self): # Animal 클래슀의 'speak' λ©”μ†Œλ“œλ₯Ό μž¬μ •μ˜ return "멍멍!" class Cat(Animal): # Animal 클래슀λ₯Ό μƒμ†λ°›μŒ def speak(self): # Animal 클래슀의 'speak' λ©”μ†Œλ“œλ₯Ό μž¬μ •μ˜ return "μ•Όμ˜Ή!" # λ‹€ν˜•μ„± μ‚¬μš© μ˜ˆμ‹œ animal = Animal() animal.speak() # 'μ–΄λ–€ μ†Œλ¦¬λ₯Ό λƒ…λ‹ˆλ‹€.' dog = Dog() dog.speak() # '멍멍!' cat = Cat() cat.speak() # 'μ•Όμ˜Ή!'

이 μ˜ˆμ œμ—μ„œ speak λ©”μ†Œλ“œλŠ” 'Animal', 'Dog', 'Cat' ν΄λž˜μŠ€μ—μ„œ 각각 λ‹€λ₯΄κ²Œ κ΅¬ν˜„λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

μ΄λ ‡κ²Œ μ„œλ‘œ λ‹€λ₯Έ 객체듀이 'speak'λΌλŠ” λ™μΌν•œ λ©”μ†Œλ“œ ν˜ΈμΆœμ— λŒ€ν•΄ λ‹€λ₯Έ 행동을 λ³΄μ΄λŠ” 것을 λ‹€ν˜•μ„±μ΄λΌκ³  ν•©λ‹ˆλ‹€.


λ‹€ν˜•μ„±μ˜ μ—­ν• 

  • 객체 μœ μ—°μ„± 증가: ν•˜λ‚˜μ˜ μΈν„°νŽ˜μ΄μŠ€(Interface, 외뢀와 μƒν˜Έμž‘μš©ν•˜λŠ” κ·œμΉ™)으둜 λ‹€μ–‘ν•œ 데이터 νƒ€μž…κ³Ό 객체λ₯Ό μ²˜λ¦¬ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

  • μ½”λ“œ μž¬μ‚¬μš©μ„±: λ‹€ν˜•μ μΈ μ½”λ“œλŠ” μž¬μ‚¬μš©ν•˜κΈ° μ‰¬μ›Œ ν”„λ‘œκ·Έλž¨μ˜ ν™•μž₯μ„±κ³Ό μœ μ§€λ³΄μˆ˜μ„±μ΄ ν–₯μƒλ©λ‹ˆλ‹€.

Guidelines

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result