Guidelines

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

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

λ‹€ν˜•μ„±μ„ ν™œμš©ν•˜λ©΄ 객체가 λ™μΌν•œ λ©”μ„œλ“œλ‚˜ μΈν„°νŽ˜μ΄μŠ€λ₯Ό 톡해 μ„œλ‘œ λ‹€λ₯Έ λ°©μ‹μœΌλ‘œ λ™μž‘ν•  수 μžˆμŠ΅λ‹ˆλ‹€.


μ½”λ“œλ‘œ μ΄ν•΄ν•˜λŠ” λ‹€ν˜•μ„±

μ•„λž˜ μ½”λ“œλŠ” Animal, Dog, Cat 클래슀λ₯Ό μ •μ˜ν•˜κ³ , 각 ν΄λž˜μŠ€μ—μ„œ speak λ©”μ„œλ“œλ₯Ό λ‹€λ₯΄κ²Œ κ΅¬ν˜„ν•˜μ—¬ λ‹€ν˜•μ„±μ„ κ΅¬ν˜„ν•©λ‹ˆλ‹€.

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

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

Dog와 Cat ν΄λž˜μŠ€λŠ” Animal 클래슀λ₯Ό μƒμ†λ°›μ•˜μœΌλ©°, speak λ©”μ„œλ“œλ₯Ό μžμ‹ λ§Œμ˜ λ°©μ‹μœΌλ‘œ μž¬μ •μ˜(μ˜€λ²„λΌμ΄λ”©)ν–ˆμŠ΅λ‹ˆλ‹€.

μ΄λ ‡κ²Œ λ™μΌν•œ λ©”μ„œλ“œ ν˜ΈμΆœμ— λŒ€ν•΄ 각 객체가 λ‹€λ₯Έ λ™μž‘μ„ λ³΄μ΄λŠ” 것을 λ‹€ν˜•μ„±μ΄λΌκ³  ν•©λ‹ˆλ‹€.


λ‹€ν˜•μ„±μ˜ μž₯점은 λ¬΄μ—‡μΈκ°€μš”?

객체 지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ—μ„œ λ‹€ν˜•μ„±μ€ λ‹€μŒκ³Ό 같은 이점을 κ°–μŠ΅λ‹ˆλ‹€.

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

  • μ½”λ“œ μž¬μ‚¬μš©μ„± ν–₯상: λ‹€ν˜•μ„±μ„ ν™œμš©ν•˜λ©΄ μ½”λ“œλ₯Ό μž¬μ‚¬μš©ν•˜κΈ° 쉽고, ν”„λ‘œκ·Έλž¨μ˜ ν™•μž₯μ„±κ³Ό μœ μ§€λ³΄μˆ˜κ°€ νŽΈλ¦¬ν•΄μ§‘λ‹ˆλ‹€.

Mission
0 / 1

객체 지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ—μ„œ λ‹€ν˜•μ„±(Polymorphism)은 무엇을 μ˜λ―Έν• κΉŒμš”?

μ—¬λŸ¬ 클래슀λ₯Ό μƒμ†λ°›λŠ” λŠ₯λ ₯

μ—¬λŸ¬ 객체λ₯Ό μƒμ„±ν•˜λŠ” λŠ₯λ ₯

ν•˜λ‚˜μ˜ λ©”μ„œλ“œλ‚˜ ν΄λž˜μŠ€κ°€ λ‹€μ–‘ν•œ λ°©μ‹μœΌλ‘œ μž‘λ™ν•˜λŠ” λŠ₯λ ₯

객체λ₯Ό μ‚­μ œν•˜λŠ” λŠ₯λ ₯

Guidelines

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result