Guidelines

객체 μ‚¬μ΄μ—μ„œ 값이 κ³΅μœ λ˜λŠ” 클래슀 λ³€μˆ˜

클래슀 λ³€μˆ˜(Class Variable)λŠ” ν΄λž˜μŠ€μ— μ†ν•œ λ³€μˆ˜λ‘œ, ν΄λž˜μŠ€μ— μ˜ν•΄ μƒμ„±λœ λͺ¨λ“  객체 μ‚¬μ΄μ—μ„œ 값이 κ³΅μœ λ©λ‹ˆλ‹€.

클래슀 λ³€μˆ˜λŠ” 클래슀 μ •μ˜ λ‚΄μ—μ„œ μ„ μ–Έλ˜λ©°, 클래슀 이름을 톡해 μ ‘κ·Όν•  수 μžˆμŠ΅λ‹ˆλ‹€.


클래슀 λ³€μˆ˜ μ‚¬μš© μ˜ˆμ‹œ

클래슀 λ³€μˆ˜ μ‚¬μš© μ˜ˆμ‹œ
class Employee: raise_amount = 1.04 # 클래슀 λ³€μˆ˜ def __init__(self, name, salary): self.name = name self.salary = salary def apply_raise(self): self.salary = int(self.salary * Employee.raise_amount) # 클래슀 λ³€μˆ˜ μ‚¬μš© μ˜ˆμ‹œ emp1 = Employee("홍길동", 50000) emp2 = Employee("λ°•μ² μˆ˜", 60000) print(Employee.raise_amount) # 1.04 print(emp1.raise_amount) # 1.04 print(emp2.raise_amount) # 1.04 Employee.raise_amount = 1.05 # 클래슀 λ³€μˆ˜ λ³€κ²½ print(emp1.raise_amount) # 1.05 (클래슀 λ³€μˆ˜κ°€ λ³€κ²½λ˜λ©΄ λͺ¨λ“  μΈμŠ€ν„΄μŠ€μ— 반영됨)

이 μ˜ˆμ œμ—μ„œ raise_amountλŠ” Employee 클래슀의 λͺ¨λ“  μΈμŠ€ν„΄μŠ€μ— 영ν–₯을 λ―ΈμΉ˜λŠ” 클래슀 λ³€μˆ˜μž…λ‹ˆλ‹€. 클래슀 λ³€μˆ˜μ˜ 값이 λ³€κ²½λ˜λ©΄, λͺ¨λ“  μΈμŠ€ν„΄μŠ€μ—μ„œ 이 λ³€κ²½ 사항이 λ°˜μ˜λ©λ‹ˆλ‹€.


클래슀 λ³€μˆ˜μ˜ νŠΉμ§•

  • 곡유: 클래슀 λ³€μˆ˜λŠ” 클래슀의 λͺ¨λ“  μΈμŠ€ν„΄μŠ€ 간에 κ³΅μœ λ©λ‹ˆλ‹€.

  • λ©”λͺ¨λ¦¬ 효율: 클래슀 λ³€μˆ˜λŠ” 각 κ°μ²΄λ§ˆλ‹€ λ³„λ„λ‘œ μ €μž₯λ˜μ§€ μ•Šμ•„ λ©”λͺ¨λ¦¬λ₯Ό 효율적으둜 μ‚¬μš©ν•©λ‹ˆλ‹€.

  • ν΄λž˜μŠ€μ—μ„œ 관리: 클래슀 λ³€μˆ˜λŠ” ν΄λž˜μŠ€μ—μ„œ κ΄€λ¦¬λ˜λ―€λ‘œ μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜μ§€ μ•Šμ•„λ„ μ ‘κ·Όν•  수 μžˆμŠ΅λ‹ˆλ‹€.

Guidelines

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result