조금 더 심화과정으로 들어가보자.
class Car() :
price_change_rate = 1.0
def __init__(self, company, details) :
self._company = company
self._details = details
def __str__(self) :
return 'str: {}-{}'.format(self._company, self._details)
def __repr__(self) :
return 'repr: {}-{}'.format(self._company, self._details)
def detail_info(self) :
print('current id : {}'.format(id(self)))
print('current price : {} - {}'.format(self._company self._details.get('price)))
def current_price(self) :
return 'current_price : {}'.format(self._details.get('price')))
def expected_price(Self) :
return 'expected_price : {}'.format(self._details.get('price')*price_change_rate)
#클래스 메쏘드들 활용해서 클래스 변수를 변화시켜 원하는 값을 출력해보자.
@classmethod
def change_rate(cls,rate) :
if rate <= 1 :
print('please input value over 1')
return
else :
cls.price_change_rate = rate
print('success')
return
@staticmethod
def is_BMW(inst) :
if inst.is_BMW = Bmw:
print('company: {}'.format(inst._company))
else :
print('sorry')
# 자동차 인스턴스
car1 = Car('Bmw', {'color' : 'Black', 'horsepower': 270, 'price': 5000})
car2 = Car('Audi', {'color' : 'Silver', 'horsepower': 300, 'price': 6000})
print(car1) =
str : Bmw - {'color': 'Black', 'horsepower': 270, 'price': 5000}
car1.detail_info() =
Current Id : 2340586381168
Car Detail Info : Bmw 5000
# 가격 인상(클래스 메소드 사용)
Car.change_rate(1.6)
print()
# 가격 정보(인상 후 : 클래스메소드)
print(car1.expected_price())
print(car2.expected_price())
print()
# 예제답안 :
After Car Price -> company : Bmw, price : 8000.0
After Car Price -> company : Audi, price : 9600.0