国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Python > 正文

python創建子類的方法分析

2024-09-09 19:03:05
字體:
來源:轉載
供稿:網友

本文實例講述了python創建子類的方法。分享給大家供大家參考,具體如下:

如果你的類沒有從任何祖先類派生,可以使用object作為父類的名字。經典類的聲明唯一不同之處在于其沒有從祖先類派生---此時,沒有圓括號:

# !/usr/bin/env python# -*- coding: utf-8 -*-class ClassicClassWithoutSuperclasses:  def fun1(self):    print 'aaaaaaa'a=ClassicClassWithoutSuperclasses()print aprint 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

至此,我們已經看到了一些類和子類的例子,下面還有一個簡單的例子:

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 子類調用它的方法calling child method>>> c.parentMethod() # calls parent's method 調用父類的方法calling parent method

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python面向對象程序設計入門與進階教程》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結》及《Python入門與進階經典教程》

希望本文所述對大家Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桃江县| 吉木乃县| 武威市| 景东| 吉隆县| 华池县| 荆门市| 德惠市| 讷河市| 和田市| 黄大仙区| 汉沽区| 榆林市| 兴山县| 济宁市| 韶山市| 泰顺县| 永清县| 双桥区| 固阳县| 尚义县| 黄冈市| 昆山市| 罗山县| 洱源县| 贵德县| 饶阳县| 潼南县| 巫溪县| 株洲县| 新化县| 临漳县| 东丰县| 二连浩特市| 邵阳县| 大姚县| 怀来县| 尤溪县| 百色市| 万源市| 石渠县|