python類和繼承用法實(shí)例
本文實(shí)例講述了python類和繼承定義與用法。分享給大家供大家參考。具體如下:
class Employee: pass lee = Employee() lee.name = 'leefang' lee.age = 28
class cAA: i = 8 def __init__(self): self.__a = 7 self.m_a = 1 self.m_b = 2 def __Method1(self): self.__a += 1 return self.__a def Method2(self, _x): print self.__Method1(), _x class cCC: def Method2(self,_x): print 'aaaaaaaaa' class cBB(cCC, cAA): def kk(self, _x): self.Method2(_x) def tt(self): print self.m_a a = cAA() a.Method2(3) print a.m_a b = cBB() b.Method2(4) b.kk('ffffff') b.tt()
類繼承如下:
#!/usr/bin/python class Oneclass: def setdata(self,value): self.data = value def display(self): print self.data class Twoclass(Oneclass): def display(self): print "current value is %s" % self.data class Threeclass(Twoclass): def __init__(self,value): self.data = value def __add__(self,other1): self.data = self.data + other1 def __mul__(self,other2): self.data = self.data * other2 object1 = Oneclass() object1.setdata(1234) object1.display() object2 = Twoclass() object2.setdata(1234) object2.display() object3 = Threeclass(12) object3.display() object3.setdata(1234) object3.display() object3 + 22 object3.display() object3 * 10 object3.display()
希望本文所述對大家的Python程序設(shè)計(jì)有所幫助。
- Python類定義和類繼承詳解
- Python實(shí)現(xiàn)類繼承實(shí)例
- Python類的多重繼承問題深入分析
- python中子類繼承父類的__init__方法實(shí)例
- python類繼承與子類實(shí)例初始化用法分析
- python繼承和抽象類的實(shí)現(xiàn)方法
- Python中類的定義、繼承及使用對象實(shí)例詳解
- Python中類型關(guān)系和繼承關(guān)系實(shí)例詳解
- 基于python3 類的屬性、方法、封裝、繼承實(shí)例講解
- Python tkinter模塊中類繼承的三種方式分析
- Python面向?qū)ο箢惱^承和組合實(shí)例分析
相關(guān)文章
如何使用python-opencv批量生成帶噪點(diǎn)噪線的數(shù)字驗(yàn)證碼
這篇文章主要介紹了如何使用python-opencv批量生成帶噪點(diǎn)噪線的數(shù)字驗(yàn)證碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12pytorch geometric的GNN、GCN的節(jié)點(diǎn)分類方式
這篇文章主要介紹了pytorch geometric的GNN、GCN的節(jié)點(diǎn)分類方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12python實(shí)戰(zhàn)串口助手_解決8串口多個(gè)發(fā)送的問題
今天小編就為大家分享一篇python實(shí)戰(zhàn)串口助手_解決8串口多個(gè)發(fā)送的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06Python使用PyCrypto實(shí)現(xiàn)AES加密功能示例
這篇文章主要介紹了Python使用PyCrypto實(shí)現(xiàn)AES加密功能,結(jié)合具體實(shí)例形式分析了PyCrypto實(shí)現(xiàn)AES加密的操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-05-05python數(shù)據(jù)類型_字符串常用操作(詳解)
下面小編就為大家?guī)硪黄猵ython數(shù)據(jù)類型_字符串常用操作(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05python ndarray數(shù)組對象特點(diǎn)及實(shí)例分享
在本篇文章里小編給大家分享的是一篇關(guān)于python ndarray數(shù)組對象特點(diǎn)及實(shí)例相關(guān)內(nèi)容,有需要的朋友們跟著學(xué)習(xí)下。2021-10-10