python 3.x 父類和子類

python 3.x中子類的繼承父類的相關方法和屬性:

class Parent(object):
    def parentMethod(self):
        print('calling parent method')


class Child(Parent):
    def childMethod(self):
        print('calling child method')

p = Parent()
p.parentMethod()

c = Child()
c.childMethod()
c.parentMethod()

 

運行結果:

calling parent method
calling child method
calling parent method

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章