zoukankan      html  css  js  c++  java
  • Python基础课:类的特殊方法

    class Rectangle:
        '这是一个矩形类'
        def __init__(self,length,width):
            if isinstance(length,(int,float)) and isinstance(width,(int,float)):
                self.length = length
                self.width  = width
            else:
                raise TypeError
                #return None
        def area(self):
            areas = self.length * self.width
            return areas
     
        def __str__(self):
            return '宽为%s,高为%s'%(self.width,self.length)
        def __repr__(self):
            return '面积为:%s'%(self.area())
        def __call__(self):
            return '这是一个call方法'
        def __add__(self,other):
            add_length = self.length + other.length
            add_width = self.width + other.width
            return add_length,add_width
     
    b = Rectangle(3,5)
    b.__dict__
    b.__doc__
     
    #print(b)
    b
     
    c = Rectangle(4,6)
  • 相关阅读:
    USACO 2021 US Open
    【UR #20】跳蚤电话
    省选前的做题记录
    PE444
    杂题
    CF1190E
    gym100299E
    杂题
    2021 5 10 团队博客
    2021 5 9 团队博客
  • 原文地址:https://www.cnblogs.com/yuebei/p/7101192.html
Copyright © 2011-2022 走看看