eobeom 发表于 2020-6-3 22:54:09

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

本帖最后由 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)打印每个正方形的位置,宽度和长度
class Rectangle:
    def __init__(self,width=0,length=0):
      self.__width=width
      self.__length=length
      

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

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

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

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

class Indicating:
    def __init__(self,x,y):
      self.__x=x
      self.__y=y
   

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

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

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

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


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



Twilight6 发表于 2020-6-3 22:58:54

你的代码乱了,重写编辑下~

eobeom 发表于 2020-6-3 23:06:41

Twilight6 发表于 2020-6-3 22:58
你的代码乱了,重写编辑下~

编辑好了 麻烦帮我看一眼谢谢

赚小钱 发表于 2020-6-3 23:07:46

本帖最后由 赚小钱 于 2020-6-3 23:09 编辑

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

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

再说回你的代码,看不懂,为什么有这么多的类,这么多的代码。

Twilight6 发表于 2020-6-3 23:08:23

eobeom 发表于 2020-6-3 23:06
编辑好了 麻烦帮我看一眼谢谢

嗯代码写麻烦了点不过自己写出来很强

Twilight6 发表于 2020-6-3 23:13:46

eobeom 发表于 2020-6-3 23:06
编辑好了 麻烦帮我看一眼谢谢

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

参考代码如下:
class Rectangle:

    def __init__(self, x, y, width=0, length=0):
      self.x = x
      self.y = y
      self.__width = width
      self.__length = length

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

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

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


rectangle1 = Rectangle(0, 0, 100, 100)
rectangle2 = Rectangle(10, 10, 200, 200)

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

print('正方形2其位置为:',rectangle2.indicating(),'大小为:',rectangle2.representing())
print('正方形2其面积为:',rectangle2.calcArea())

冬雪雪冬 发表于 2020-6-3 23:13:55

class Rectangle:
    def __init__(self, indicating = (0, 0), representing = (0, 0)):
      self.indicating = indicating
      self.representing = representing
    def calcArea(self):
      return self.representing * self.representing
a = Rectangle((0, 0), (100, 100))
b = Rectangle((0, 0), (200, 200))
print("正方形的位%s, 宽度%s,长度%s"%(a.indicating, a.representing, a.representing))
print("正方形的位%s, 宽度%s,长度%s"%(b.indicating, b.representing, b.representing))

eobeom 发表于 2020-6-3 23:22:01

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

参考 ...

我明白了,谢谢谢谢{:10_287:}{:10_287:}{:10_287:}

Twilight6 发表于 2020-6-3 23:22:46

eobeom 发表于 2020-6-3 23:22
我明白了,谢谢谢谢

那么就给个最佳把~{:10_281:}
页: [1]
查看完整版本: 大佬帮我看看 要创建一个代表矩形的类 这样写对吗