python继承多重继承
生活随笔
收集整理的這篇文章主要介紹了
python继承多重继承
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一,基本語(yǔ)法
class MyClass(BaseClass):def __init__(self):print('...') class MyDefineClass(object):def __init__(self):print('繼承自object類')MyDefineClass.__init__(None) # 屬性訪問(wèn) me = MyDefineClass() # 實(shí)例對(duì)象''' 繼承自object類 繼承自object類 '''二,父類在其它模塊
class MyClass(ModuleName.BaseClass):def __init__(self):print('...')- 相同目錄下的Father模塊的father類
- Extend模塊
- 單繼承
extend模塊
class people:# 基本屬性name = ''age = ''count = 0 # 類變量# 私有屬性,類的外部無(wú)法訪問(wèn)__height = 0def __init__(self, name, age, height):self.name = nameself.age = ageself.__height = heightpeople.count += 1print(self.__height)def show(self):print(f'I am {self.name}, age {self.age}, height {self.__height}', people.count)def test_people():p = people('jack', 13, 170)p.show()p1 = people('rose', 17, 176)p1.show()father模塊
# 單繼承調(diào)用父類的構(gòu)造函數(shù) from Father import people class student(people):grade = ''def __init__(self, name, age, height, grade):# 掉用父類的__init__函數(shù)people.__init__(self, name, age, height)self.grade = grade# 覆寫父類的方法def show(self):print(f'I am {self.name}, age {self.age} and in {self.grade}')def test_stu():s = student('rye', 19, 168, 'colleage three')s.show() # test_stu()''' 168 # people類里面的身高 I am rye, age 19 and in colleage three '''三,python 多繼承
- 可以繼承多個(gè)類
- 調(diào)用父類方法優(yōu)先搜索左邊的類(下面有demo)
-
盡量不要用多重繼承!!!
-
只會(huì)降低性能,一個(gè)個(gè)的找父類的方法
-
還會(huì)把代碼搞復(fù)雜
四,方法重寫
- super(type, self) type 子類 self是子類的實(shí)例;如果在類里面使用 直接super()
五,私有屬性
-
類的私有屬性
私有屬性
self.__attribute, 只能在本身類使用
私有方法
self.__method_name, 只能在本來(lái)使用 -
私有屬性
- 私有方法
總結(jié)
以上是生活随笔為你收集整理的python继承多重继承的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: qmenu只在鼠标单击时消失_两种方法解
- 下一篇: c++ 数组的输入遇到特定字符停止输入_