python3.3 类与继承 小例

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.add(999)
oChild.addtwice(4)
print(oChild.data)
print(oChild.plus(2,3))


注意:这里的 self 相当于java或C#中的this   

             __init__  中的下划线是2个,而不是一个


效果图:

你可能感兴趣的:(python3.3 类与继承 小例)