python初始化方法
阿新 • • 發佈:2018-12-11
python可以用__init__函式進行初始化,子類中可以使用super函式和__init__進行初始化
#encoding:utf-8
class Person(object):
def __init__(self,name,sex):
self.name = name
self.sex = sex
class Child(Person): # Child 繼承 Person
def __init__(self,name,sex,mother,father):
Person. __init__(self,name,sex) # 子類對父類的構造方法的呼叫
self.mother = mother
self.father = father
class Child_chg(Person): # Child 繼承 Person
def __init__(self,name,sex,mother,father):
# Person.__init__(self,name,sex) # 子類對父類的構造方法的呼叫
super( Child_chg, self).__init__(name,sex)
self.mother = mother
self.father = father
# class Child_chg(Person): # Child 繼承 Person
# def __init__(self,name,sex,mother,father):
# # Person.__init__(self,name,sex) # 子類對父類的構造方法的呼叫
# super(Child_chg, self).__init__()
# self.mother = mother
# self.father = father
May = Child("May","female","April","June")
print(May.name,May.sex,May.mother,May.father)
May_chg = Child_chg("May","female","April","June")
print(May_chg.mother,May_chg.father)