0

我在 Python 编程课上有一个实验作业,但遇到了一些麻烦。我们的导师给了我们源代码 GeometricObject.py 作为参考。

class GeometricObject(object):
  def __init__(self, color = "white", filled = True):
    self.color = color
    self.filled = filled

  def getColor(self):
    return self.color

  def setColor(self, color):
    self.color = color

  def isFilled(self):
    return self.filled

  def setFilled(self, filled):
    self.filled = filled

  def __str__(self):
    return "color: " + self.color + \
        " and filled: " + str(self.filled)

现在我应该使用这个类来创建一个将这些信息应用于不同对象的子类。我必须创建一个类来计算圆形和三角形的面积和周长。

我无法理解何时需要从源代码复制和粘贴或不需要。这是我制作的第一节课,涉及一个圆圈。简单的问题:它的命名是否正确?该类是否应称为 GeometricObject(circle):???

import math

class GeometricObject(object):
  '''Class using a Circle'''
  def __init__(self, color = "white", filled = True):
    self.radius = 1
    self.color = color
    self.filled = filled

  def getColor(self):
    return self.Color

  def setColor(self, color):
    self.color = color

  def isFilled(self):
    return self.filled

  def setFilled(self, filled):
    self.filled = filled

  def getArea(self):
    self.area = math.pi(self.radius)**2
    return self.area

  def getPerimeter(self):
    r = radius
    self.perimeter = 2(math.pi)(r)
    return self.perimeter

  def __str__(self):
    return "color: " + self.color + \
        " and filled: " + str(self.filled)

注意:在 def_str_方法中,它应该打印 Circle: radius = 3 color: red andfilled: True" 尽管它之前声明将默认值设置为 1,颜色:白色。我很困惑这个。

这就是它的要点。我希望我的编码到目前为止是正确的!

然后我必须为 Triangle 类做同样的事情,但我很确定它是相似的。

class GeometricObject(object):
  '''Class using a Triangle'''
  def __init__(self, color = "white", filled = True):
    self.side1 = 1
    self.side2 = 1
    self.side3 = 1

  def getArea(self):
    s = (self.side1 + self.side2 + self.side3)/2
    area = math.sqrt(s(s - self.side1)(s - self.side2)(s - self.side3))
    return area

  def getPerimeter(self)
    perimeter = self.side1 +self.side2 + self.side3
    return perimeter

  def __str__(self):
    return "color: " + self.color + \
        " and filled: " + str(self.filled)

  def getColor(self):
    return self.Color

  def setColor(self, color):
    self.color = color

  def isFilled(self):
    return self.filled

  def setFilled(self, filled):
    self.filled = filled

所以我想我的主要问题是:+我应该一遍又一遍地从 GeometricObject() 复制粘贴源代码,还是已经存在?+字符串方法让我感到困惑,因为它要求的值与以前指定的不同。+代码。我想我正确地实现了每个的面积和周长公式?

提前致谢。任何帮助是极大的赞赏!!

4

3 回答 3

1

我想你想这样做:

class Circle(GeometricObject):
  .
  .
  .

class Triangle(GeometricObject):
  .
  .
  .

您只需要在与 GeometricObject 类不同的 Circle 和 Triangle 类中实现那些东西。

于 2012-09-25T05:04:01.630 回答
0

In order to use the GeometricObject.py file you're going to want to import the file so that you can access it's methods, make sure that both files are in the same directory:

import GeometricObject from GeometricObject

Put that at the top of your current file if you wish to keep it separate. You can also simply copy-paste the source code from that file as well if you wish. Next, you're going to want to create the class of GeometricObject. In Python, it is implied that any class that does not have a specified inheritance has object as it's superclass. The cleanest way to write out this problem would be simply:

class GeometricObject:

instead of

class GeometricObject(object):

All following classes that wish to inherit the methods in the GeometricObject class would be written like this. You must include

super().__init__() 

in your initializer for your subclass in order to initialize all the data fields in the GeometricObject superclass so that you can use them. That way you can delete the isFilled and getColor and setColor methods out of your Triangle class and keep them in your GeometricObject class.

class Triangle(GeometricObject):
    def __init__(self, color = "white", filled = True):
        super().__init__()
    -
    -
    -
class Circle(GeometricObject):
    -
    -
    -

Also, keep an eye out for

area = math.sqrt(s(s - self.side1)(s - self.side2)(s - self.side3))

This will return an error. In Python (x)(x) does not mean x * x. This instead will need to be written out like:

area = math.sqrt(s*(s - self.side1)*(s - self.side2)*(s - self.side3))
于 2014-12-02T02:54:12.520 回答
0

Vaughn 对几何对象的子类化有正确的想法,但请检查您在 getArea 函数上的工作:

s(s - self.side1)

将引发以下回溯:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
于 2012-09-25T05:20:17.633 回答