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
>>>

上一篇:Altium Designer 设置多层方法及各层介绍


下一篇:RecyclerView StaggeredGridLayoutManager瀑布流边框刷新问题,错位解决