日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python继承多重继承

發(fā)布時間:2023/12/10 python 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python继承多重继承 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一,基本語法

class MyClass(BaseClass):def __init__(self):print('...') class MyDefineClass(object):def __init__(self):print('繼承自object類')MyDefineClass.__init__(None) # 屬性訪問 me = MyDefineClass() # 實(shí)例對象''' 繼承自object類 繼承自object類 '''

二,父類在其它模塊

class MyClass(ModuleName.BaseClass):def __init__(self):print('...')
  • 相同目錄下的Father模塊的father類

# father模塊 class father(object):var = 99def __init__(self):print('父類構(gòu)造函數(shù)。')def raise_son(self):print('我要養(yǎng)育我的孩子。')def show(self):print('i am your dad~')
  • Extend模塊
from Father import fatherclass Child1(father):def __init__(self):print("繼承father模塊的Father類")def show(self):print('i am your son~')def test_child1():ch = Child1()ch.raise_son() # 擁有父類的非私有方法ch.show() # 擁有父類的方法, 被重寫后就是自己的方法啦。print(ch.var) # 擁有父類的屬性test_child1() ''' 繼承father模塊的Father類 我要養(yǎng)育我的孩子。 i am your son~ 99 '''
  • 單繼承

extend模塊

class people:# 基本屬性name = ''age = ''count = 0 # 類變量# 私有屬性,類的外部無法訪問__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 多繼承

  • 可以繼承多個類
class MyClass(base1, base2, base3):def __init__(self):print('...')
  • 調(diào)用父類方法優(yōu)先搜索左邊的類(下面有demo)
# 一個父類 class speaker():topic = ''name = ''def __init__(self, topic, name):self.topic = topicself.name = namedef show(self):print(f'I am a speecher, name {self.name}, my topic is {self.topic}')# sample 繼承speaker 和 student,見上面的student class sample(speaker, student): # speaker的方法優(yōu)先def __init__(self, topic, name, age, height, grade):speaker.__init__(self, topic, name) student.__init__(self, name, age, height, grade)def test_sample():T = sample('python', 'jack', 25, 180, 10)T.show() # 多重繼承里面方法調(diào)用優(yōu)先級是括號左邊的父類# test_sample() ''' 180 people中打印的身高 I am a speecher, name jack, my topic is python '''
  • 盡量不要用多重繼承!!!

  • 只會降低性能,一個個的找父類的方法

  • 還會把代碼搞復(fù)雜

四,方法重寫

  • super(type, self) type 子類 self是子類的實(shí)例;如果在類里面使用 直接super()
# 重寫 class parent:def say(self):print('調(diào)用父類方法')class child(parent):def say(self):print('調(diào)用子類方法')def test_overwirte():c = child()c.say() # 子類實(shí)例使用父類方法super(child, c).say() # 子類調(diào)用父類覆蓋的方法# test_overwirte() ''' 調(diào)用子類方法 調(diào)用父類方法 '''

五,私有屬性

  • 類的私有屬性
    私有屬性
    self.__attribute, 只能在本身類使用
    私有方法
    self.__method_name, 只能在本來使用

  • 私有屬性

# 私有屬性 class MyCounter():__private_count = 0public_count = 0def count(self):self.__private_count += 1self.public_count += 1print('私有變量 ', self.__private_count)def test_count():x = MyCounter()x.count()x.count()print('public 變量', x.public_count)# print(x.__private_count) 私有變量不能訪問 ''' 私有變量 1 私有變量 2 public 變量 2 '''
  • 私有方法
class website:def __init__(self, name, url):self.name = nameself.__url = urldef show(self):print('name ', self.name)print('url ', self.__url)def __func(self):print('這是私有方法')def func(self):self.__func() # 私有方法只能本類調(diào)用print('這是共有方法')def test_site():w = website('csdn', 'csdn.net')w.func()w.show()# w.__func() # AttributeError: 'website' object has no attribute '__func' test_site() ''' 這是私有方法 這是共有方法 name csdn url csdn.net '''

總結(jié)

以上是生活随笔為你收集整理的python继承多重继承的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。