πŸ¦• 곡룑이 되자!

2BPerfect...6 λ³Έλ¬Έ

Development/Python

2BPerfect...6

Kirok Kim 2022. 1. 24. 15:28
객체와 클래슀
## 정적 λ©”μ„œλ“œ

정적 λ©”μ„œλ“œλŠ” ν΄λž˜μŠ€μ™€ 관련이 μžˆμ–΄μ„œ 클래슀 μ•ˆμ— λ‘κΈ°λŠ” ν•˜μ§€λ§Œ ν΄λž˜μŠ€λ‚˜
클래슀의 μΈμŠ€ν„΄μŠ€μ™€λŠ” λ¬΄κ΄€ν•˜κ²Œ λ…λ¦½μ μœΌλ‘œ λ™μž‘ν•˜λŠ” ν•¨μˆ˜λ₯Ό λ§Œλ“€κ³  μ‹Άμ„λ•Œ 이용
ν•¨μˆ˜λ₯Ό μ •μ˜ν• λ•Œ 인자둜 selfμ‚¬μš© x μ •μ λ©”μ„œλ“œ μ•ˆμ—μ„œ μΈμŠ€ν„΄μŠ€ λ©”μ„œλ“œλ‚˜
λ³€μˆ˜μ— μ ‘κ·Όν•  수 μ—†λ‹€. ν•¨μˆ˜ μ•žμ— λ°μ½”λ ˆμ΄ν„°μΈ @staticmethodλ₯Ό μ„ μ–Έν•˜μ—¬ μ •μ λ©”μ„œλ“œ ν‘œν˜„

class Car():
	def __init__(self,size,color):
	def move(self,speed):
	def auto_cruise(self):

## 정적 λ©”μ„œλ“œ μ˜ˆμ‹œ
@staticmethod
def check_type(model_code):
	if(model_code>=20):
		print('이 μžλ™μ°¨λŠ” μ „κΈ°μ°¨μž…λ‹ˆλ‹€.')
	elif(10<=model_code<20):
		print('이 μžλ™μ°¨λŠ” κ°€μ†”λ¦°μ°¨μž…λ‹ˆλ‹€.')
	else:
		print('이 μžλ™μ°¨λŠ” λ””μ €μ°¨μž…λ‹ˆλ‹€.')

## 클래슀 λ©”μ„œλ“œ

@classmethod
def count_instance(cls):
	print('μžλ™μ°¨ 객체의 개수 :{0}'.format(cls.instance_count))

클래슀 λ©”μ„œλ“œλŠ” 클래슀 λ³€μˆ˜λ₯Ό μ‚¬μš©ν•˜κΈ° μœ„ν•œ ν•¨μˆ˜
클래슀 λ©”μ„œλ“œλŠ” ν•¨μˆ˜λ₯Ό μ •μ˜ν•  λ•Œ 첫 번째 인자둜 클래슀λ₯Ό λ„˜κ²¨λ°›λŠ” clsκ°€ ν•„μš”
클래슀 λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜κΈ° μœ„ν•΄μ„œλŠ” @classmethodλ₯Ό 지정해야함

클래슀 λ©”μ„œλ“œλŠ” μƒμ„±λœ 객체의 개수λ₯Ό λ°˜ν™˜ν•˜λŠ” λ“± 클래슀 μ „μ²΄μ—μ„œ 관리해야 ν•  κΈ°λŠ₯이 μžˆμ„ λ•Œ
주둜 이용

λ°˜μ‘ν˜•

'Development > Python' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

2BPerfect...8  (0) 2022.01.25
2BPerfect...7  (0) 2022.01.24
2BPerfect...5  (0) 2022.01.24
2BPerfect...4  (0) 2022.01.24
2BPerfect...3  (0) 2022.01.22
Comments