热线电话:13121318867

登录
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.0000
2
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子