鱼C论坛

 找回密码
 立即注册
查看: 1011|回复: 8

[已解决]大佬帮我看看 要创建一个代表矩形的类 这样写对吗

[复制链接]
发表于 2020-6-3 22:54:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 eobeom 于 2020-6-3 23:05 编辑

创建一个代表矩形的calss
class Rectangle包含以下变量和函数
representing代表矩形宽度和高度的变量
indicating指示矩形位置的变量
之后构造函数
返回矩形的面积calcArea()
1)定义上面的class Rectangle
2)创建一个矩形对象,其位置为(0,0),大小为(100,100)
3)创建一个矩形对象,其位置为(10,10),大小为(200,200)。
4)打印每个正方形的位置,宽度和长度
  1. class Rectangle:
  2.     def __init__(self,width=0,length=0):
  3.         self.__width=width
  4.         self.__length=length
  5.         

  6.     def setWidth(self,width):
  7.         self.__width=width

  8.     def setLength(self,length):
  9.         self.__height=height

  10.     def getcalcArea(self):
  11.         return self.__width*self.__length

  12.     def __str__(self):
  13.         return '(%d,%d)'%(self.__width,self.__length)

  14. class Indicating:
  15.     def __init__(self,x,y):
  16.         self.__x=x
  17.         self.__y=y
  18.    

  19.     def setX(self,x):
  20.         self.__x=x

  21.     def setY(self,y):
  22.         self.__y=y

  23.     def __str__(self):
  24.         return'(%d,%d)'%(self.__x,self.__y)

  25. rectangle=Rectangle(100,100)
  26. indicating=Indicating(0,0)
  27. print('位置是:',indicating,'大小为;',rectangle)
  28. print('矩形的面积是:',rectangle.getcalcArea())


  29. rectangle=Rectangle(200,200)
  30. indicating=Indicating(10,10)
  31. print('位置是:',indicating,'大小为;',rectangle)
  32. print('矩形的面积是:',rectangle.getcalcArea())

复制代码


最佳答案
2020-6-3 23:13:46
eobeom 发表于 2020-6-3 23:06
编辑好了 麻烦帮我看一眼谢谢

题目中只叫我们创建一个 Rectangle 的 矩形类 ,所以你只需要创造一个类即可,其他的设置为方法

参考代码如下:
  1. class Rectangle:

  2.     def __init__(self, x, y, width=0, length=0):
  3.         self.x = x
  4.         self.y = y
  5.         self.__width = width
  6.         self.__length = length

  7.     def representing(self):
  8.         return (self.__width, self.__length)

  9.     def indicating(self):
  10.         return (self.x, self.y)

  11.     def calcArea(self):
  12.         return self.__width * self.__length


  13. rectangle1 = Rectangle(0, 0, 100, 100)
  14. rectangle2 = Rectangle(10, 10, 200, 200)

  15. print('正方形1其位置为:',rectangle1.indicating(),'大小为:',rectangle1.representing())
  16. print('正方形1其面积为:',rectangle1.calcArea())

  17. print('正方形2其位置为:',rectangle2.indicating(),'大小为:',rectangle2.representing())
  18. print('正方形2其面积为:',rectangle2.calcArea())
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-6-3 22:58:54 | 显示全部楼层
你的代码乱了,重写编辑下~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-3 23:06:41 | 显示全部楼层
Twilight6 发表于 2020-6-3 22:58
你的代码乱了,重写编辑下~

编辑好了 麻烦帮我看一眼谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-3 23:07:46 | 显示全部楼层
本帖最后由 赚小钱 于 2020-6-3 23:09 编辑

如果你的矩形的边,都是平行于坐标轴
那只需要一组对角顶点坐标即可
或者左下角的顶点(任意定点,还需要知道方向,很烦),加上长宽数值。

否则的话, 建议
按照顺时针,或者逆时针
使用四个字段,保存四个点的错标
毕竟,我猜你不会想,每一次使用都去计算矩形的位置关系。

再说回你的代码,看不懂,为什么有这么多的类,这么多的代码。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-3 23:08:23 | 显示全部楼层
eobeom 发表于 2020-6-3 23:06
编辑好了 麻烦帮我看一眼谢谢

嗯  代码写麻烦了点  不过自己写出来很强
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-3 23:13:46 | 显示全部楼层    本楼为最佳答案   
eobeom 发表于 2020-6-3 23:06
编辑好了 麻烦帮我看一眼谢谢

题目中只叫我们创建一个 Rectangle 的 矩形类 ,所以你只需要创造一个类即可,其他的设置为方法

参考代码如下:
  1. class Rectangle:

  2.     def __init__(self, x, y, width=0, length=0):
  3.         self.x = x
  4.         self.y = y
  5.         self.__width = width
  6.         self.__length = length

  7.     def representing(self):
  8.         return (self.__width, self.__length)

  9.     def indicating(self):
  10.         return (self.x, self.y)

  11.     def calcArea(self):
  12.         return self.__width * self.__length


  13. rectangle1 = Rectangle(0, 0, 100, 100)
  14. rectangle2 = Rectangle(10, 10, 200, 200)

  15. print('正方形1其位置为:',rectangle1.indicating(),'大小为:',rectangle1.representing())
  16. print('正方形1其面积为:',rectangle1.calcArea())

  17. print('正方形2其位置为:',rectangle2.indicating(),'大小为:',rectangle2.representing())
  18. print('正方形2其面积为:',rectangle2.calcArea())
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-3 23:13:55 | 显示全部楼层
  1. class Rectangle:
  2.     def __init__(self, indicating = (0, 0), representing = (0, 0)):
  3.         self.indicating = indicating
  4.         self.representing = representing
  5.     def calcArea(self):
  6.         return self.representing[0] * self.representing[1]
  7. a = Rectangle((0, 0), (100, 100))
  8. b = Rectangle((0, 0), (200, 200))
  9. print("正方形的位%s, 宽度%s,长度%s"%(a.indicating, a.representing[0], a.representing[0]))
  10. print("正方形的位%s, 宽度%s,长度%s"%(b.indicating, b.representing[0], b.representing[0]))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-3 23:22:01 | 显示全部楼层
Twilight6 发表于 2020-6-3 23:13
题目中只叫我们创建一个 Rectangle 的 矩形类 ,所以你只需要创造一个类即可,其他的设置为方法

参考 ...

我明白了,谢谢谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-3 23:22:46 | 显示全部楼层
eobeom 发表于 2020-6-3 23:22
我明白了,谢谢谢谢

那么就给个最佳把~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-21 05:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表