Guidelines

ν΄λž˜μŠ€μ™€ μΈμŠ€ν„΄μŠ€ μ˜ˆμ‹œ

이번 μˆ˜μ—…μ—μ„œλŠ” "은행 κ³„μ’Œ"λ₯Ό 예둜 λ“€μ–΄ 클래슀(Class)와 μΈμŠ€ν„΄μŠ€(Instance)(λ˜λŠ” 객체)의 관계λ₯Ό μ‚΄νŽ΄λ³΄κ² μŠ΅λ‹ˆλ‹€.

μ•„λž˜ μ½”λ“œλŠ” 'BankAccount' 클래슀λ₯Ό μ •μ˜ν•˜κ³ , μ—¬λŸ¬ κ³„μ’Œ μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜μ—¬ 각 κ³„μ’Œμ˜ κ³ μœ ν•œ 속성과 λ™μž‘μ„ κ΄€λ¦¬ν•˜λŠ” 방법을 λ³΄μ—¬μ€λ‹ˆλ‹€.


클래슀 μ„€λͺ…

BankAccount ν΄λž˜μŠ€λŠ” 은행 κ³„μ’Œλ₯Ό λ‚˜νƒ€λ‚΄λ©°, λ‹€μŒκ³Ό 같은 속성과 λ©”μ„œλ“œλ₯Ό ν¬ν•¨ν•©λ‹ˆλ‹€.

  • 속성: owner(μ†Œμœ μž), balance(μž”μ•‘)

  • λ©”μ„œλ“œ: deposit(μž…κΈˆ), withdraw(좜금), display_balance(μž”μ•‘ 쑰회)

(__init__)은 클래슀λ₯Ό λ°”νƒ•μœΌλ‘œ κ³„μ’Œ μΈμŠ€ν„΄μŠ€κ°€ 생성될 λ•Œ ν˜ΈμΆœλ˜λŠ” μƒμ„±μž λ©”μ„œλ“œλ‘œ, κ³„μ’Œ 객체에 μ†Œμœ μž(owner)와 초기 μž”μ•‘(balance)을 μ„€μ •ν•©λ‹ˆλ‹€.

μž”μ•‘μ€ κΈ°λ³Έκ°’μœΌλ‘œ 0(balance=0)을 κ°–μŠ΅λ‹ˆλ‹€.

BankAccount 클래슀
class BankAccount: # μƒμ„±μž λ©”μ„œλ“œ def __init__(self, owner, balance=0): self.owner = owner self.balance = balance # μž…κΈˆ λ©”μ„œλ“œ def deposit(self, amount): self.balance += amount print(f"{amount}κ°€ μž…κΈˆλ˜μ—ˆμŠ΅λ‹ˆλ‹€.") # 좜금 λ©”μ„œλ“œ def withdraw(self, amount): if self.balance >= amount: self.balance -= amount print(f"{amount}κ°€ μΆœκΈˆλ˜μ—ˆμŠ΅λ‹ˆλ‹€.") else: print("μž”μ•‘μ΄ λΆ€μ‘±ν•©λ‹ˆλ‹€.") # μž”μ•‘ 쑰회 λ©”μ„œλ“œ def display_balance(self): print(f"κ³„μ’Œ μž”μ•‘: {self.balance}")

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

μ•„λž˜λŠ” BankAccount 클래슀λ₯Ό μ‚¬μš©ν•˜μ—¬ κ³„μ’Œ μΈμŠ€ν„΄μŠ€ account1κ³Ό account2λ₯Ό μƒμ„±ν•˜κ³ , 각 κ³„μ’Œμ— λŒ€ν•΄ μž…κΈˆ, 좜금, μž”μ•‘ 쑰회λ₯Ό μˆ˜ν–‰ν•˜λŠ” μ˜ˆμ œμž…λ‹ˆλ‹€.

클래슀 μ‚¬μš© μ˜ˆμ‹œ
# μΈμŠ€ν„΄μŠ€ 생성 account1 = BankAccount("μ½”λ“œν”„λ Œμ¦ˆ", 1000) account2 = BankAccount("κΈ±ν•˜μš°μŠ€", 2000) # account1에 λŒ€ν•œ μž…κΈˆ, 좜금, μž”μ•‘ 쑰회 account1.deposit(500) # 500이 μž…κΈˆλ˜μ—ˆμŠ΅λ‹ˆλ‹€. account1.withdraw(200) # 200이 μΆœκΈˆλ˜μ—ˆμŠ΅λ‹ˆλ‹€. account1.display_balance() # κ³„μ’Œ μž”μ•‘: 1300 # account2에 λŒ€ν•œ μž…κΈˆ, 좜금, μž”μ•‘ 쑰회 account2.deposit(1000) # 1000이 μž…κΈˆλ˜μ—ˆμŠ΅λ‹ˆλ‹€. account2.withdraw(500) # 500이 μΆœκΈˆλ˜μ—ˆμŠ΅λ‹ˆλ‹€. account2.display_balance() # κ³„μ’Œ μž”μ•‘: 1500

각 κ³„μ’Œ(account1, account2)λŠ” BankAccount 클래슀의 μΈμŠ€ν„΄μŠ€λ‘œ, 독립적인 속성(owner, balance)κ³Ό λ©”μ„œλ“œ(deposit, withdraw, display_balance)λ₯Ό κ°–μŠ΅λ‹ˆλ‹€.

μ΄λ ‡κ²Œ 클래슀λ₯Ό 톡해 μƒμ„±λœ μΈμŠ€ν„΄μŠ€λ“€μ€ 각자의 데이터λ₯Ό μœ μ§€ν•˜λ©°, λ™μΌν•œ λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜λ”λΌλ„ 각 μΈμŠ€ν„΄μŠ€μ— 따라 λ‹€λ₯Έ κ²°κ³Όλ₯Ό λ‚˜νƒ€λƒ…λ‹ˆλ‹€.

예λ₯Ό λ“€μ–΄ account κ³„μ’Œμ˜ display_balance λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•˜λ©΄ μž”μ•‘μœΌλ‘œ 1300이 좜λ ₯λ˜μ§€λ§Œ, account2 κ³„μ’Œμ˜ display_balance λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•˜λ©΄ μž”μ•‘μœΌλ‘œ 1500이 좜λ ₯λ©λ‹ˆλ‹€.


self ν‚€μ›Œλ“œλŠ” λ¬΄μ—‡μΈκ°€μš”?

νŒŒμ΄μ¬μ—μ„œ selfλŠ” 클래슀의 λ©”μ„œλ“œμ—μ„œ ν˜„μž¬ μΈμŠ€ν„΄μŠ€λ₯Ό μ°Έμ‘°ν•˜λŠ” ν‚€μ›Œλ“œμž…λ‹ˆλ‹€.

클래슀 λ‚΄μ—μ„œ λ©”μ„œλ“œλ₯Ό μ •μ˜ν•  λ•Œ, 첫 번째 λ§€κ°œλ³€μˆ˜λ‘œ selfλ₯Ό μ‚¬μš©ν•˜μ—¬ ν˜„μž¬ μΈμŠ€ν„΄μŠ€μ— μ ‘κ·Όν•  수 μžˆμŠ΅λ‹ˆλ‹€.

self μ£Όμš” 역할은 λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

  • μΈμŠ€ν„΄μŠ€ 속성 μ ‘κ·Ό: selfλ₯Ό μ‚¬μš©ν•˜μ—¬ 클래슀 λ‚΄μ˜ λ©”μ„œλ“œμ—μ„œ ν•΄λ‹Ή μΈμŠ€ν„΄μŠ€μ˜ 속성에 μ ‘κ·Όν•˜κ³  μˆ˜μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, self.balanceλŠ” ν˜„μž¬ μΈμŠ€ν„΄μŠ€μ˜ balance 속성을 μ°Έμ‘°ν•©λ‹ˆλ‹€.

  • λ©”μ„œλ“œ 호좜: 같은 μΈμŠ€ν„΄μŠ€ λ‚΄μ—μ„œ λ‹€λ₯Έ λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•  λ•Œ selfλ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, self.deposit(amount)λŠ” 같은 μΈμŠ€ν„΄μŠ€ λ‚΄μ˜ deposit λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•©λ‹ˆλ‹€.

Mission
0 / 1

λΉˆμΉΈμ— λ“€μ–΄κ°ˆ κ°€μž₯ μ μ ˆν•œ λ‹¨μ–΄λŠ” λ¬΄μ—‡μΌκΉŒμš”?

ν΄λž˜μŠ€λŠ” λ₯Ό μƒμ„±ν•˜κΈ° μœ„ν•œ μ„€κ³„λ„μž…λ‹ˆλ‹€.
μΈμŠ€ν„΄μŠ€
λ©”μ„œλ“œ
속성
ν•¨μˆ˜

Guidelines

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result