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 代表以后写代码       

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