Python/Python 연습

04. Class 01

HicKee 2023. 1. 18. 23:54
# 1 ~ 2 자동차 클래스 작성 - 제조사 색상 가격 모델명
"""
class Car:
    def __init__(self, company='기아', model='K7', color='블랙', price='사천'):
        self.__company = company
        self.__model = model
        self.__color = color
        self.__price = price

    def car_info(self):
        print('회사 : ', self.__company, end=' ')
        print('모델 : ', self.__model, end=' ')
        print('색상 : ', self.__color, end=' ')
        print('가격 : ', self.__price, end=' ')

    def car_name(self, name):
        self.__model = name

    #getter
    @property
    def price(self):
        return self.__price

    #setter
    @price setter
    def price(self,new_price):
        self.__price = new_price

if __name__ == '__main__':
    ca1 = Car('현대', '그랜저', '흰색', '4천')
    ca1.car_info()
    ca2 = Car()

    ca2.car_info()
    ca3 = Car()

    ca3.car_name('몰라')
    ca3.car_info()

    ca3.price = '3천만'

'Python > Python 연습' 카테고리의 다른 글

04. Class 03  (0) 2023.01.22
04. Class 02  (0) 2023.01.21
03. function  (0) 2023.01.17
02. if ~ else, for  (0) 2023.01.17
01. Data Type  (0) 2023.01.17