当前位置:网站首页>freeCodeCamp----shape_calculator练习
freeCodeCamp----shape_calculator练习
2022-04-23 05:57:00 【Lily的秋天】
目录
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
版权声明
本文为[Lily的秋天]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_34285232/article/details/121357848
边栏推荐
猜你喜欢
随机推荐
js更改全部变量失败后改成return方法,终于解决解决问题
New features of ES6
ES6新增方法
.Net Core 下使用 Quartz —— 【2】作业和触发器之初步了解作业
Assembly base code example
Detailed explanation and application principle of token
Detailed explanation and application of PN junction and diode principle
Incremental update of client software
服务器常见错误代码 总结
ASP.NET CORE配置选项(下篇)
HDU-Tunnel Warfare
Use of C language and
Header内置对象
WebSocket(基础)
各进制数之间的互相转换
todesk远程控制软件的使用
自用学习笔记-connectingString配置
Node数据流
v-for下定时给图片添加动画
WebAPI+Form表单上传文件









