2019-03-26
阅读量:
526
如何使用super访问子类中的父成员?
我们也可以使用super访问父类成员
# Python example to show that base
# class members can be accessed in
# derived class using super()
class Base(object):
# Constructor
def __init__(self, x):
self.x = x
class Derived(Base):
# Constructor
def __init__(self, x, y):
''' In Python 3.x, "super().__init__(name)"
also works'''
super(Derived, self).__init__(x)
self.y = y
def printXY(self):
# Note that Base.x won't work here
# because super() is used in constructor
print(self.x, self.y)
# Driver Code
d = Derived(10, 20)
d.printXY()
输出:
(10,20)






评论(0)


暂无数据
推荐帖子
0条评论
0条评论
0条评论