Python繼承總結

#conding=utf-8
class Person(object):
  eyes = 2
  def __init__(self,name,age):
     self.n = name
     self.a =age
  def say(self,w):
     print("%s said eating %s"%(self.name,w))
p1 = Person('robert',17)
p1.favourate = "eating huiguorou"
del p1.favourate;
p2 = Person('liming',15)
p2.sex = 1
del p2.sex
p2.good = "is_good"
del p2.good
p1.n = 'einstein'
print("p1 's attribute:",p1.__dict__)
print("p2 's attribute:",p2.__dict__)

class qiaoguozhong(Person):
  def __init__(self,name1,age1):
    super().__init__(name1,age1)
    self.son = "father's son"
  def son_print(self):
    print(self.n + "'s attribute is : " + self.a)
qi = qiaoguozhong('qiaoguoz','57')
qi.son_print()    






 

 

(1)子類用下列語句初始化後,那麼可以使用父類的所有對象了  父類的self對象,全局對象,方法等。父類的這些屬性和方法子類全部繼承,可以使用了。目前沒發現子類不能繼承的東東。包括類屬性eyes也可以繼承。

(2)定義類的實例的時候,一定要注意  real_example =  CLASS_NAME(parameter1,paramter2,...,parametern)一定要有括號

如果建立的時候,不需要形參,那麼括號也必須有,不過就是形參列表空的罷了

def __init__(self,attribute1,attrribute2,...,attributen):
    super().__init__(attribute1,attribute3,...,attributen)

(3)定義一個類,如果沒有想好代碼,那麼可以用:

class B(object):
    pass
        
 

pass 代表以後寫代碼       

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章