class 클래스이름:
@staticmethod
def 메서드(매개변수1, 매개변수2):
코드
class Calc:
@staticmethod
def add(a, b):
print(a + b)
@staticmethod
def mul(a, b):
print(a * b)
Calc.add(10, 20) #클래스에서 바로 메서드 호출
Calc.mul(20, 30) #클래스에서 바로 메서드 호출
30
600
class 클래스이름:
@classmethod
def 메서드(cls, 매개변수1, 매개변수2):
코드
class Person:
count = 0 # 클래스 속성
def __init__(self):
Person.count += 1
@classmethod
def print_count(cls):
print('{0}명 생성되었습니다'.format(cls.count))
hyerim = Person()
hyeyoung = Person()
nahyun = Person()
Person.print_count()
3명 생성되었습니다.
class Date:
@staticmethod
def is_date_valid(date_string):
year, month, day = map(int, date_string.split('-'))
return month <= 12 and day <= 31
if Date.is_date_valid('2000-12-31'):
print('올바른 날짜 형식입니다.')
else:
print('잘못된 날짜 형식입니다.')
올바른 날짜 형식입니다.
class Time:
def __init__(self, hour, minute, second):
self.hour = hour
self.minute = minute
self.second = second
@staticmethod
def is_time_valid(time_string):
hour, minute, second = map(int, time_string.split(':'))
return hour <= 24 and minute <= 59 and second <= 60
@classmethod
def from_string(cls, time_string):
hour, minute, second = map(int, time_string.split(':'))
time = cls(hour, minute, second)
return time
time_string = input()
if Time.is_time_valid(time_string):
t = Time.from_string(time_string)
print(t.hour, t.minute, t.second)
else:
print('잘못된 시간 형식입니다.')
<참고>