沒什麼可說的,繼承主要就是繼承父類的一些方法,**中很詳細
#!/usr/bin/env python
#coding:utf-8
class father(object):#新式類
def __init__(self):
self.name='liu'
self.familyname='yan'
def lee(self):
print '我是父類函式lee'
def allen(self):
print "我是父類函式allen"
class son(father):
def __init__(self):
#father.__init__(self) #經典類執行父類建構函式
super(son,self).__init__() #新式類執行父類建構函式
self.name='feng'
def aswill(self): #子類新增函式
print 'son.bar'
def lee(self):#重寫父類函式lee
print '子類重寫了父類函式lee'
s1=son()
print "繼承了父類的姓"+ s1.familyname
print "重寫了父類的名字",s1.name
s1.lee() #子類重寫了父類函式lee
s1.allen() #子類繼承了父類函式allen
繼承多個類時的順序,經典類繼承是深度優先,是乙個bug, 新式類是廣度優先,應該是用新式類去定義類
新式類class a(object): #新式類的寫法
def __init__(self):
print 'this is from a'
def test(self):
print 'this is test from a'
class b(a):
def __init__(self):
print "this is from b"
class c(a):
def __init__(self):
print "this is from c"
def test(self):
print "this is test from c"
class d(b,c):
def __init__(self):
print 'this is d'
t1=d()
t1.test()經典類
class a():
def __init__(self):
print 'this is from a'
def test(self):
print 'this is test from a'
class b(a):
def __init__(self):
print "this is from b"
class c(a):
def __init__(self):
print "this is from c"
def test(self):
print "this is test from c"
class d(b,c):
def __init__(self):
print 'this is d'
t1=d()
t1.test()
python 面向繼承物件 繼承
1.python物件導向的繼承指的是多個類之間的所屬關係,即預設繼承父類的所有屬性和方法。2.注意 當乙個類有多個父類的時候,預設使用第乙個父類的同名屬性和方法。3.檢視某個類的繼承關係 物件.mro 4.子類呼叫父類的同名方法和屬性 class a object definit self self...
python 物件導向繼承
coding utf 8 author martin date 2017 10 15 class f def f1 self print f.f1 class s f deff1 self super s,self f1 執行父類的方法,第一種方式,常用這種方式 f.f1 self 執行父類的方法,...
python物件導向 繼承
多繼承class animal def eat self print 吃 def drink self print 喝 def run self print 跑 def sleep self print 睡 class dog animal def bark self print 汪汪叫 dog d...