알파카징징이 알파카징징이 코딩하는 알파카

데이터 구조 및 분석 ch_2_4 Polymorphism and Abstract Class

» writing

KAIST 산업및시스템공학과 문일철_ 데이터 구조 및 분석 수업을 참고하여 작성하였습니다

ch_2_4 Polymorphism and Abstract Class

정의


Polymorphism and Abstract Class

1. Polymorphism (다양한 모양)
   - Poly : many
   - Morph : shape
   - Different behaviors with similar signature
   - signature : Method name + Parameter list
   - sub1) Method Overriding
      : Base class has a method num.
      its derived class has a method num.
   - sub2) Method Overloading
      : A class has a method num.
      num, name & num, name, home
class Building:
   strAddress = "Daejeon"
   def openDoor(self):
      print("Door opened")

class Hotel :
   def openDoor(self):
      print("Bellboy opens a door")
   def checkIn(self):
      print("Someone checks in for 1 day")
   def checkIn(self, days = 1):
      print("Someone checks in for", days, "days")

lotteHotel = Hotel()
lotteHotel.openDoor()
lotteHotel.checkIn()
lotteHotel.checkIn(2)

# Bellboy opens a door
# Someone checks in for 1 days
# Someone checks in for 2 days
2. Abstract Class
   - A class with an abstract method
   - What is the abstract method?
      : Method with signature, but with no implementation
   - Abstract class is not a complete
      implementation, it is more like a half-made produce
   - you can't make an instance out of it
   - The concrete class with full implementations and inheriting
   the abstract class will b a basis for instatnces
import abc
class Room(object) :
   __metaclass__ = abc.ABCMeta

   # --> Indicator of abstract base method and class
   def openDoor(self):
      pass
   def oepnWindow(self):
      pass

class BedRoom(Room) :
   def openDoor(self):
      print("Open bedroom door")
   def oepnWindow(self):
      print("Open bedroom window")

class Lobby(Room):
   def openDoor(self):
      print("Open lobby door")
room1 = BedRoom()
print(issubclass((BedRoom, Room), isinstance(room1, Room)))
lobby1 = Lobby()
print(issubclass(Lobby, Room), isinstance(lobby1, Room))

# TypeError: issubclass() arg 1 must be a class
3. Overriding Methods in object
   - All of python classes are the descendants of object
   - If you don't specify the base class of your class,
      then your class is the direct derived class of object
   - object has many hidden methods
      : __init__ 
      : __del__
      : __eq__
      : __cmp__
      : __add__
   - override them to make the methods behave as you please
class Room:
   numWidth = 100
   numHeight = 100
   numDepth = 100
   def __init__(self, parWidth, parHeight, parDepth):
      self.numDepth = parDepth
      self.numWidth = parWidth
      self.numHeight = parHeight
   def getVolumn(self):
      return self.numDepth * self.numHeight * self.numWidth
   def __eq__(self, other):
      if isinstance(other, Room):
         if self.getVolumn() == other.getVolumn():
            # Duck Typing (막 타이핑)
            # Easier to Ask for Forgiveness then Permission
            return True
      return False
room1 = Room(100, 20, 30)
room2 = Room(100, 10, 60)
print(room1 == room2)

#True