Python

Python Class 메소드 오버라이딩(Method override) 예제

with_AI 2022. 4. 14. 16:07

Notebook이라는 Class 만들기

# Notebook 이라는 사물을 클래스로 정의합니다.
class Notebook():
  def __init__(self, manufacturer, model, cpu_type, ram_size, ssd_size):
    self.manufacturer = manufacturer
    self.model = model
    self.cpu_type = cpu_type
    self.ram_size = ram_size
    self.ssd_size = ssd_size

 

생성자 __init__안에 자기 자신 self, 제조사, 모델, cpu 타입, ram , ssd 와 같은걸 넣어서 class만듬

 

# 클래스의 생성자(constructor)를 불러와봅니다.
notebook = Notebook("Apple", "M1 Max", "M1 Max", 64, 2048)
notebook.model

 

notebook이라는 클래스 선언

 

 

클래스 안에 함수 구현

내부 함수는 운영체제가 무엇인지 판단

is_UNIX라는 함수는 class의 os를 확인

# 클래스 내부의 함수를 구현합니다.
# 해당 노트북의 운영체제가 UNIX 계열 운영체제인지 아닌지 확인하는 Class Method를 구현합니다.

class Notebook:
  
  def __init__(self, manufacturer, model, cpu_type, ram_size, ssd_size, os):
    self.manufacturer = manufacturer
    self.model = model
    self.cpu_type = cpu_type
    self.ram_size = ram_size
    self.ssd_size = ssd_size
    self.os = os

  ## TO-DO ##
  def is_UNIX(self):
    UNIX = ["macOS", "Ubuntu", "iOS", "Android"]
    if self.os in UNIX:
      return True
    else:
      return False

nb = Notebook("Apple", "M1 Max", "M1 Max", 64, 2048, "macOS")
nb2 = Notebook("LG", "Gram 17", "Intel i7", 16, 512, "Windows")

#nb.is_UNIX()
nb2.is_UNIX()

 

 

모델을 출력하는 print_model 함수 구현

 

# 해당 노트북의 모델을 출력해주는 Class Method를 구현합니다.
class Notebook:
  
  def __init__(self, manufacturer, model, cpu_type, ram_size, ssd_size, os):
    self.manufacturer = manufacturer
    self.model = model
    self.cpu_type = cpu_type
    self.ram_size = ram_size
    self.ssd_size = ssd_size
    self.os = os

  ## TO-DO ##
  def is_UNIX(self):
    UNIX = ["macOS", "Ubuntu", "iOS", "Android"]
    if self.os in UNIX:
      return True
    else:
      return False

  def print_model(self):
    print(f"This Notebook is {self.model} model.")

nb = Notebook("Apple", "M1 Max", "M1 Max", 64, 2048, "macOS")
nb2 = Notebook("LG", "Gram 17", "Intel i7", 16, 512, "Windows")
nb.print_model()
nb2.print_model()

 

상속

 

MackBook이라는 Class는 Notebook Class를 상속받음

# notebook class를 상속받아서 새로운 MacBook class를 정의
class MacBook(Notebook):
  def __init__(self, model, release_date, display_size, cpu_type, ram_size, ssd_size, os):
    self.model = model
    self.release_date = release_date
    self.display_size = display_size
    self.cpu_type = cpu_type
    self.ram_size = ram_size
    self.ssd_size = ssd_size
    self.os = os

macbook = MacBook("Macbook Pro", "2020", "13", "Intel i5", 16, 1024, "macOS")
macbook.print_model() # print_model <- Notebook 클래스에서 상속받은 Class method.

그래서 Notebook class 내부 함수인 print_model이 동작함

 

Dell_laptop Class도 선언

# notebook class를 상속받아서 새로운 Dell_Laptop class를 정의
class Dell_Laptop(Notebook):
  def __init__(self, model, series, display_size, cpu_type, ram_size, ssd_size, os="Windows 11"):
    self.model = model
    self.series = series
    self.display_size = display_size
    self.cpu_type = cpu_type
    self.ram_size = ram_size
    self.ssd_size = ssd_size
    self.os = os

dell = Dell_Laptop("XPS", 9500, 15, "Intel i7", 16, 2048, "Windows 10")
dell.print_model()

 

메소드 오버라이딩

 

Notebook class 의 is_UNIX 함수를 다시 만들어서 재정의 하는 것을 메소드 오버라이딩이라고 한다.

 

is_UNIX함수의 UNIX 변수 리스트를 더 추가해서 검사하는 걸로 변경하였다.

 

print_model도 메소드 오버라이딩으로 좀 더 상세한 출력이 가능하도록 하였다.

# 각 모델마다 다르게 정보를 출력해주는 is_UNIX 함수를 재정의합니다.
# notebook class를 상속받아서 새로운 MacBook class를 정의
class MacBook(Notebook):
  def __init__(self, model, release_date, display_size, cpu_type, ram_size, ssd_size, os):
    self.model = model
    self.release_date = release_date
    self.display_size = display_size
    self.cpu_type = cpu_type
    self.ram_size = ram_size
    self.ssd_size = ssd_size
    self.os = os

  def is_UNIX(self):
    UNIX = ["Ubuntu", "iOS", "Android", "Mojave", "Catelina", "Sierra" ,"High Sierra", "Montrery"]
    if self.os in UNIX:
      return True
    else:
      return False

# notebook class를 상속받아서 새로운 Dell_Laptop class를 정의
class Dell_Laptop(Notebook):
  def __init__(self, model, series, display_size, cpu_type, ram_size, ssd_size, os="Windows 11"):
    self.model = model
    self.series = series
    self.display_size = display_size
    self.cpu_type = cpu_type
    self.ram_size = ram_size
    self.ssd_size = ssd_size
    self.os = os

  def print_model(self):
    print(f"This notebook is {self.model} {self.series} {self.display_size} model.")


macbook = MacBook("Macbook Pro", "2020", "13", "Intel i5", 16, 1024, "Mojave")
print(macbook.is_UNIX())

dell = Dell_Laptop("XPS", 9500, 15, "Intel i7", 16, 2048, "Mojave")
#dell.print_model()
print(dell.is_UNIX())

'Python' 카테고리의 다른 글

데이터 시각화 라이브러리 Seaborn  (0) 2022.04.19
Pandas를 사용해야 하는 이유  (0) 2022.04.15
Numpy array  (0) 2022.04.14
Python Class 기초  (0) 2022.04.14
OOP 기초  (0) 2022.04.14