freeCodeCamp----shape_calculator练习

目录

1 题目要求

1.1 Rectangle class

1.2 Square class

2 测试输入

3 源码分析


1 题目要求

要求创建一个Rectangle类和一个Square类,后者是前者的子类,继承前者的方法和属性。

类的实现也给得很清楚,基本就是把要求翻译成python就OK了,具体如下:

1.1 Rectangle class

创建Rectangle类时,需要包含以下方法:

1.set_width
2.set_height
3.get_area: Returns area (width * height)
4.get_perimeter: Returns perimeter (2 * width + 2 * height)
5.get_diagonal: Returns diagonal ((width ** 2 + height ** 2) ** .5)
# get_picture函数在当宽度小于50时,将矩形的面积用*号打印出来
# 超过50要打印错误信息
# 每一行记得添加换行符
6.get_picture():
# get_amount_inside函数,接收一个正方形参数,返回矩形里可以通过正方形的个数
# 举例:一个4*8的矩形里面可以包含两个边长为4*4的正方形
7.get_amount_inside();

当Rectangle调用为一个字符串时,给定形式为:Rectangle(width=5, height=10)

1.2 Square class

创建Square类时,有以下要求:

1.Square是Rectangle的子类;
2.创建Square类时,传入一个边长参数;
3.__init__();方法应该存入从Rectangle中继承过来的width和height属性;
4.Square类可以调用Rectangle方法,并包含一个set_side()方法;
5.当Square调用为一个字符串时,给定形式为:Square(side=9)
6.set_width和set_height方法定义width和height;

2 测试输入

rect = shape_calculator.Rectangle(10, 5)
print(rect.get_area())
rect.set_height(3)
print(rect.get_perimeter())
print(rect)
print(rect.get_picture())

sq = shape_calculator.Square(9)
print(sq.get_area())
sq.set_side(4)
print(sq.get_diagonal())
print(sq)
print(sq.get_picture())

rect.set_height(8)
rect.set_width(16)
print(rect.get_amount_inside(sq))

期望输出

50
26
Rectangle(width=10, height=3)
**********
**********
**********

81
5.656854249492381
Square(side=4)
****
****
****
****

8

3 源码分析

没啥好说的,全部是定义,只有一个打印函数也不是很难,多试几次就出来了。

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height

    def set_width(self, width):
        self.width = width

    def set_height(self, height):
        self.height = height

    def get_area(self):
        return self.width * self.height;

    def get_perimeter(self):
        return 2 * self.width + 2 * self.height;

    def get_diagonal(self):
        return (self.width ** 2 + self.height ** 2) ** .5;

    def get_picture(self):
        if self.width > 50 or self.height > 50:
            return "Too big for picture."
        else:
            a = ''
            for i in range(self.height):
                # for j in range(self.width):
                # print('*')
                a += '*' * self.width + '\n'
            # print('\n')
            return a

    def __str__(self):
        return 'Rectangle(width={}, height={})'.format(self.width, self.height)

    def get_amount_inside(self, another_shape):
        return (self.width // another_shape.width) * (self.height // another_shape.height)


class Square(Rectangle):
    def __init__(self, side):
        self.width = side
        self.height = side

    def set_side(self, side):
        self.width = side
        self.height = side

    def __str__(self):
        return 'Square(side={})'.format(self.width)

    def set_width(self, side):
        self.width = side
        self.height = side

    def set_height(self, side):
        self.width = side
        self.height = side

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值