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

데이터 구조 및 분석 ch_2_3 Encapsulation and Inheritance

» writing

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

ch_2_3 Encapsulation and Inheritance

정의


Encapsulation and Inheritance

1. Encapsulation
  1) Object = Data + Behavior
   - Data : field, member variable, attribute
   - Behavior : method, member function, operation
  2) Delagating the implementation responsibility (구현책임)
  3) Utilizing the visibility
   - private : seen only within the class
   - protected : only within the class & its descendants
   - public : everywhere
  4) Python doens't suppport the visibility options

전체 설계 건축가 <---------class 정의----------> 인테리어 디자이너
구현 구분,, interface as a specifiction

2. Inheritance
   1) Inheritance
    - Giving my attributes to my descendants
    - My attributes = Member variables, Methods
    - My descendants may have new attributes of their own
    - My descendants may mask the received attributes
    - But if not specified, sons follow their father
   2) Superclass
    - My ancestors, like father
    - Generalized from the conceptual view
   3) Subclass
    - My descendants, like son
    - Specialized from the conceptual view
   4) How about having a mother?
    - possible in python

Person
- login()
 ||
 ||
 \/
Customer
- login()
- ID
Employee
- login()

3. Inheritance in Python
1) Base Class(Super Class)
   - Father, Mother
2) Multiple inheritnace
   - Child
   - me :: child
3) See child has father's, mother's attributes
4) See child overwrite father's method by his own
class Father(object):
   strHometown = "jeju"
   def __init__(self):
      print("Father is created")
   def doFatherThing(self):
      print("Fahter's actions")
   def doRunning(self):
      print("Slow")

class Mother(object):
   strHometown = "Seoul"
   def __init__(self):
      print("Mother is created")
   def doMotherThing(self):
      print("Mother's actions")

class Child(Father, Mother):
   strName = "Moon"
   def __init__(self):
      super(Child, self).__init__()
      print("Child is created")
   def doRunning(self):
      print("Fast")

me = Child()
me.doFatherThing()
me.doMotherThing()
me.doRunning()
print(me.strHometown)
print(me.strName)

# Father is created
# Child is created
# Fahter's actions
# Mother's actions
# Fast
# jeju
# Moon
4. self and super
   - self : reference variable pointing the instance itself
   (instance 자기자신!)
   - super : reference variable pointing the base class instance
   (상속받는 class)
   (is used to call the base class methods)
1) Referring father to point father's attributes
2) Referring itself to point its attributes
class Father(object):
   def __init__(self, paraHome):
      self.strHometown = paraHome
      print("Father is created")
   def doFatherThing(self):
      print("Fahter's actions")
   def doRunning(self):
      print("Slow")

class Mother(object):
   strHometown = "Seoul"
   def __init__(self):
      print("Mother is created")
   def doMotherThing(self):
      print("Mother's actions")

class Child(Father, Mother):
   strName = "Moon"
   def __init__(self, paraName, paraHome):
      super(Child, self).__init__(paraHome)
      self.paraName = paraHome
      print("Child is created")
   def doRunning(self):
      print("Fast")

me = Child("sun", "Universe")
me.doFatherThing()
me.doMotherThing()
me.doRunning()
print(me.strHometown)
print(me.strName)

# Father is created
# Child is created
# Fahter's actions
# Mother's actions
# Fast
# Universe
# Moon