728x90
class Monitor:
# 필드 선언
# __ private 성질은 같은 클래스 내에서만 접근 가능하다.
#__company = ""
#__inch = 0
#__price = 0
#__color = ""
# 파이썬에서는 1개 이상의 생성자를 만들 수 없다!!
# C++ 에 Overloading 이라는 개념이 존재하긴 한다.
# -> 매개변수의 타입과 개수에 따라서 같은 이름의 메서드라도 다른 메서드가 호출이 되는 형태를 지칭한다.
# 매개변수가 4개 존재하는 생성자
def __init__(self, company, inch, price, color):
# self.company 는 멤버변수를 칭하는 것이며, company는 외부에서
# 생성자를 호출할 때 매개변수 값을 들어오는 것을 의미
self.__company = company
self.__inch = inch
self.__price = price
self.__color = color
# getter() 메서드
def getCompany(self):
return self.__company
def getInch(self):
return self.__inch
def getPrice(self):
return self.__price
def getColor(self):
return self.__color
# setter() 메서드
def setCompany(self,company):
self.__company = company
def setInch(self, inch):
self.__inch = inch
def setPrice(self, price):
self.__price = price
def setColor(self, color):
self.__color = color
def __str__(self):
print("제조사 : ", self.__company)
print("인치 : ", self.__inch)
print("가격 : ", self.__price)
print("생상 : ", self.__color)
# __company와 company 는 엄연히 다른 변수명이며, __는 캡슐화 기능이 존재한다.
# __init__() 생성자에서 바로 필드 생성이 가능하다.
배운 점:
__init__() 생성자에서 바로 필드 생성이 가능하다!!
__company와 company 는 엄연히 다른 변수명이며, __ 는 캡슐화 기능이 존재한다.
파이썬은 1개의 생성자만 이용 가능하다!
C++ 에는 Overloading 의 개념이 존재하여 같은 이름의 함수 생성이 가능하다. 매개변수의 type과 개수에 따라 같은 이름의 메서드라도 다른 메서드가 호출되는 기능이다.
'Python > 객체지향 프로그래밍' 카테고리의 다른 글
(Python) 객체지향 프로그래밍 - 인스턴스 변수와 클래스 변수 (1) | 2024.01.15 |
---|---|
(Python) 객체지향 프로그래밍 - self (0) | 2024.01.10 |
(Python) 객체지향 프로그래밍 - 은닉화, 캡슐화 (0) | 2024.01.06 |
(Python) 객체지향 프로그래밍 - 생성자 (0) | 2024.01.06 |
(Python) 객체지향 프로그래밍 - 클래스 (0) | 2024.01.05 |