class 클래스명:

      pass( 아무것도 정의하지 않을시 )

 

class 안에서 정의한 변수들은 static 영역이 된다 (따로 클래스.변수 로 호출 안해도 됨)

RED = 0
GREEN = 1
PURPLE = 2

@staticmethod 로 스태틱메서드 구현가능

 @staticmethod
def calc(fruit, qty):
    print('해당 과일 %d개 구매시 총 가격은 %d원입니다.' % (qty, fruit.price * qty))

 __init__() 함수
- 생성자
- 첫번째 인값으로 self를 받아야한다 (this 역할)

def __init__(self, name, price):
self.name = name
self.price = price

apple = Fruit("사과", 1500)
grape = Fruit('포도', 3500)

apple.printInfo()
grape.printInfo()


클래스의 static 값 활용하기

print(Fruit.RED)
print(Fruit.GREEN)
print(Fruit.PURPLE)

Fruit.calc(apple, 5)
Fruit.calc(grape, 17)

'PYTHON' 카테고리의 다른 글

def (함수)  (0) 2024.10.02
Set  (0) 2024.10.02
Dicionary  (0) 2024.09.23
Sequence  (2) 2024.09.23
Tuple  (0) 2024.09.19

+ Recent posts