注意,类中的方法,self这个参数是必须的。
>>> class fraction:
def __init__(self,top,bottom):
self.num = top
self.den = bottom
def show():
print("%d / %d"%(self.num,self.den))
>>> my = fraction(3,5)
>>> my.show()
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
my.show()
TypeError: show() takes 0 positional arguments but 1 was given
>>> class fraction1:
def __init__(self,top,bottom):
self.num = top
self.den = bottom
def show(self):
print("%d / %d"%(self.num,self.den))
>>> my = fraction1(3,5)
>>> my.show()
3 / 5
>>>