python类的初始化方法和类的init方法:

2019年3月29日00:18:46 发表评论 2,021 views
class Rectangle:
    def __init__(self,length,weight):#初始化方法
        self.length = length
        self.weight=weight
    def area(self):
        return self.length * self.weight
    def __repr__(self):
        return str(self.length*self.weight)

class square(Rectangle):
    def __call__(self):#call方法实现类的名称可调用
       print((self.length+self.weight)*2)#调用时打印边长

sq = square(20,33)
print(sq)
sq()#类的实例可调用


结果:
660
106

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: