■ @classmethod 데코레이터를 사용해 클래스 정적 메소드를 만드는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
class TestClass: staticVariable = 0 # 정적 변수 def __init__(self, value): self.instanceVariable = value @classmethod def staticMethod(cls, value): cls.staticVariable += value print(f"staticVariable : {cls.staticVariable}") testClass1 = TestClass(1) testClass2 = TestClass(2) TestClass.staticMethod(10) testClass1.staticMethod(5) testClass2.staticMethod(3) """ staticVariable : 10 staticVariable : 15 staticVariable : 18 """ |