python算法与数据结构(第一部分:入门)2

注意,类中的方法,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
>>>

posted @ 2021-05-07 09:45  一二三的三二一  阅读(52)  评论(0)    收藏  举报