python創(chuàng)建子類的方法分析
本文實例講述了python創(chuàng)建子類的方法。分享給大家供大家參考,具體如下:
如果你的類沒有從任何祖先類派生,可以使用object作為父類的名字。經(jīng)典類的聲明唯一不同之處在于其沒有從祖先類派生---此時,沒有圓括號:
# !/usr/bin/env python # -*- coding: utf-8 -*- class ClassicClassWithoutSuperclasses: def fun1(self): print 'aaaaaaa' a=ClassicClassWithoutSuperclasses() print a print type(a) print a.fun1()
C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/eeeee/a5.py
<__main__.ClassicClassWithoutSuperclasses instance at 0x0047BDF0>
<type 'instance'>
aaaaaaa
None
至此,我們已經(jīng)看到了一些類和子類的例子,下面還有一個簡單的例子:
class Parent(object): # define parent class 定義父類 def parentMethod(self): print 'calling parent method
# !/usr/bin/env python # -*- coding: utf-8 -*- class Parent(object): # define parent class 定義父類 def parentMethod(self): print 'calling parent method' class Child(Parent): # define child class 定義子類 def childMethod(self): print 'calling child method' a=Parent() # instance of parent 父類的實例 print a.parentMethod()
C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/eeeee/a5.py
calling parent method
None
>>> c = Child() # instance of child 子類的實例 >>> c.childMethod() # child calls its method 子類調(diào)用它的方法 calling child method >>> c.parentMethod() # calls parent's method 調(diào)用父類的方法 calling parent method
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python面向?qū)ο蟪绦蛟O(shè)計入門與進階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
python根據(jù)開頭和結(jié)尾字符串獲取中間字符串的方法
這篇文章主要介紹了python根據(jù)開頭和結(jié)尾字符串獲取中間字符串的方法,涉及Python操作字符串截取的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03Python selenium鍵盤鼠標(biāo)事件實現(xiàn)過程詳解
這篇文章主要介紹了Python selenium鍵盤鼠標(biāo)事件實現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07python 采用paramiko 遠程執(zhí)行命令及報錯解決
這篇文章主要介紹了python 采用paramiko 遠程執(zhí)行命令及報錯解決,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10Django對接elasticsearch實現(xiàn)全文檢索的示例代碼
搜索是很常用的功能,如果是千萬級的數(shù)據(jù)應(yīng)該怎么檢索,本文主要介紹了Django對接elasticsearch實現(xiàn)全文檢索的示例代碼,感興趣的可以了解一下2021-08-08