Wie führe ich eine Summe (Zugabe) mit Werten aus einer Klasse aus?Python

Python-Programme
Anonymous
 Wie führe ich eine Summe (Zugabe) mit Werten aus einer Klasse aus?

Post by Anonymous »

Ich habe Code, den ich gegeben habe: < /p>

Code: Select all

import math

class shape(object):
"""Shape is an base class, which is largely intended to be abstract, that
is only used as a parent class to other classes which define specific shapes
and inherit the shape class' fuctions getArea() and printArea().
"""

def __init__(self):
'''constructor function for the shape class
'''
self.area = None
self.name = "shape"

def getArea(self):
'''returns the area of the shape, which is defined in the constructor
'''
return self.area

def printArea(self):
'''prints a statement identifying the shape by its name and giving the
area of the shape. Returns None.
'''
print "The area of this " + self.name + " is: " + str(self.area)
return None
< /code>
Ich kann keines der oben genannten ändern. Ich wurde beauftragt, auf dieser Klasse aufzubauen: < /p>
class rectangle(shape):
"""A new class 'rectangle' that will inherit functions from the class
'shape'.
"""

def __init__(self, width, height):
"""A constructor function for the class.
"""
self.name = "rectangle"
self.area = width * height

r = rectangle(2, 3)

r.printArea()

class triangle(shape):
"""A new class 'triangle' that will inherit functions from the class
'shape'.
"""

def __init__(self, width, height):
"""A constructor function for the class.
"""
self.name = "triangle"
self.area = (width * height) / 2.0

t = triangle(3, 4)

t.printArea()
< /code>
Jetzt muss ich die Bereiche des Rechtecks ​​und Dreiecks außerhalb der Klassen zusammenfassen. Wie würde ich das machen? Mein Versuch ist drastisch falsch und ich weiß nicht wirklich, was ich damit mache: < /p>
def sumAreaOfShapes(shapeList):
"""The sum of all the areas of the shapes in the list.
"""
addition = str(t.getArea()) + str(r.getArea())
return addition

print sumAreaOfShapes([rectangle(1.5, 7), triangle(7, 12)])
< /code>
Alles, was tut, ist buchstäblich eine Nummer am Ende eines anderen zu kleben.def sumAreaOfShapes(shapeList):
"""The sum of all the areas of the shapes in the list.
"""
addition = (float(t.getArea()))+(float(c.getArea()))+(float(r.getArea()))
return addition

print sumAreaOfShapes([circle(100), rectangle(100, 100), triangle(100, 100)])
Ich bekomme jetzt eine Antwort, aber es ist ungefähr 90,00, daher summiert es tatsächlich die Werte aus der Klasse und nicht aus meinem Druck.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post