Guidelines

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

클래슀(Class)와 μΈμŠ€ν„΄μŠ€(Instance)의 관계λ₯Ό λ³΄μ—¬μ£ΌλŠ” μ˜ˆμ‹œλ‘œ "은행 κ³„μ’Œ"λ₯Ό λ“€μ–΄λ³΄κ² μŠ΅λ‹ˆλ‹€.

μ˜ˆμ‹œμ—μ„œλŠ” 'BankAccount' 클래슀λ₯Ό μ •μ˜ν•˜κ³ , μ—¬λŸ¬ κ³„μ’Œ μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜μ—¬ 각 κ³„μ’Œμ˜ 독립적인 속성과 행동을 κ΄€λ¦¬ν•©λ‹ˆλ‹€.

  1. 클래슀 μ„ μ–Έ (BankAccount): 이 ν΄λž˜μŠ€λŠ” 은행 κ³„μ’Œλ₯Ό λ‚˜νƒ€λƒ…λ‹ˆλ‹€. 각 κ³„μ’ŒλŠ” μ†Œμœ μž(owner)와 μž”μ•‘(balance)을 μ†μ„±μœΌλ‘œ κ°–κ³ , μž…κΈˆ(deposit)κ³Ό 좜금(withdraw) 같은 행동을 λ©”μ„œλ“œλ‘œ μ •μ˜ν•©λ‹ˆλ‹€.

  2. μ΄ˆκΈ°ν™” λ©”μ„œλ“œ (__init__): κ³„μ’Œ μΈμŠ€ν„΄μŠ€ 생성 μ‹œ μ†Œμœ μž 이름과 초기 μž”μ•‘μ„ μ„€μ •ν•©λ‹ˆλ‹€.

  3. 속성: owner (κ³„μ’Œ μ†Œμœ μž), balance (κ³„μ’Œ μž”μ•‘).

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

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}") # μΈμŠ€ν„΄μŠ€ 생성 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

이 μ½”λ“œμ—μ„œ BankAccount ν΄λž˜μŠ€λŠ” κ³„μ’Œ 객체λ₯Ό μƒμ„±ν•˜κΈ° μœ„ν•œ ν‹€λ‘œ μ‚¬μš©λ©λ‹ˆλ‹€.

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

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

참고둜 selfλŠ” νŒŒμ΄μ¬μ—μ„œ 클래슀의 λ©”μ„œλ“œ λ‚΄μ—μ„œ ν˜„μž¬ μΈμŠ€ν„΄μŠ€λ₯Ό μ°Έμ‘°ν•˜κΈ° μœ„ν•΄ μ‚¬μš©λ˜λŠ” ν‚€μ›Œλ“œμž…λ‹ˆλ‹€. 클래슀 λ‚΄μ—μ„œ λ©”μ„œλ“œλ₯Ό μ •μ˜ν•  λ•Œ, 첫 번째 λ§€κ°œλ³€μˆ˜λ‘œ selfλ₯Ό μ‚¬μš©ν•˜μ—¬ ν˜„μž¬ μΈμŠ€ν„΄μŠ€μ— μ ‘κ·Όν•©λ‹ˆλ‹€. self의 μ£Όμš” μš©λ„λŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

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

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

Guidelines

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result