class and inheritance
class Base:
def __init__(self):
self.data = []
def add(self, x):
self.data.append(x)
def addtwice(self, x):
self.add(x)
self.add(x)
# Child extends Base
class Child(Base):
def plus(self,a,b):
return a+b
oChild=Child()
oChild.add("str1")
oChild.addtwice("up");
print oChild.data
print oChild.plus(2,3)
print str(type(oChild.data))
print type(oChild.data)
U:\code>python tt.py
['str1', 'up', 'up']
5
<type 'list'>
<type 'list'>
本文展示了一个简单的Python类和继承的例子。定义了基类Base,包含添加元素的方法,并通过子类Child扩展了额外的功能。文章通过代码演示了如何使用这些类来操作数据。
1004

被折叠的 条评论
为什么被折叠?



