python 创建子类_python创建子类的方法分析
本文實(shí)例講述了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
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 父類的實(shí)例
print a.parentMethod()
C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/eeeee/a5.py
calling parent method
None
>>> c = Child() # instance of child 子類的實(shí)例
>>> c.childMethod() # child calls its method 子類調(diào)用它的方法
calling child method
>>> c.parentMethod() # calls parent's method 調(diào)用父類的方法
calling parent method
希望本文所述對大家Python程序設(shè)計有所幫助。
總結(jié)
以上是生活随笔為你收集整理的python 创建子类_python创建子类的方法分析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: rider连接mysql数据库_GitH
- 下一篇: 二类卡20万额度用完怎么办 二类卡的20