zoukankan      html  css  js  c++  java
  • Python基础课:实现一个正方形类的加减乘除

     1 class Square:
     2     def __init__(self, wh):  #因为是正方形, 只取一条边的长度
     3         if isinstance(wh,(int,float)):
     4             self.wh = wh
     5         else:
     6             raise TypeError
     7 
     8     def __add__(self, other):
     9         self_area = self.wh * self.wh     #计算self面积
    10         other_area = other.wh * other.wh  #计算other面积
    11         return self_area + other_area     #相加 x+y
    12 
    13     def __sub__(self,other):
    14         self_area = self.wh * self.wh     #计算self面积
    15         other_area = other.wh * other.wh  #计算other面积
    16         return self_area - other_area     #相减 x-y
    17     
    18     def __mul__(self,other):
    19         self_area = self.wh * self.wh     #计算self面积
    20         other_area = other.wh * other.wh  #计算other面积
    21         return self_area * other_area     #相乘 x*y
    22     
    23     def __mod__(self,other):    
    24         self_area = self.wh * self.wh     #计算self面积
    25         other_area = other.wh * other.wh  #计算other面积
    26         return self_area % other_area     #相除 x%y
    27 
    28 s1 = Square(3)
    29 s2 = Square(4)
  • 相关阅读:
    srum.3
    进程调度
    Scrum 项目——1
    《构建之法》—第6-7章
    团队合作——2
    团队合作——1
    作业调度_3
    作业调度——实验2
    典型用户
    【软件工程】做汉堡包
  • 原文地址:https://www.cnblogs.com/yuebei/p/7101182.html
Copyright © 2011-2022 走看看