1. Es gibt geparsten Text, der Forminformationen enthält. 3 verschiedene Formen sind möglich: Kreis, Rechteck, Dreieck.Erstellen Sie verschiedene Objekte durch geparsten Text
Der geparste Parameter haben die folgende Form:
['circle', [['id', '11'], ['color', 11403055], ['x', '10'], ['y', '10'], ['radius', '20']]]
['rectangle', [['id', '2'], ['color', 10494192], ['x', '10'], ['y', '20'], ['width', '10'], ['height', '10']]]
['triangle', [['id', '7'], ['color', 16716947], ['ax', '50'], ['ay', '15'], ['bx', '15'], ['by','40'], ['cx', '100'], ['cy', '100']]]
2. Die drei Formklassen erben von Basisklasse 'Shape':
class Shape(object):
def __init__ (self, id, color, x, y):
self.__id = id
self.__color = color
self.__p = g.Point2d(x, y)
class Circle(Shape):
def __init__ (self, id, color, x, y, radius):
self.__type = "circle"
self.__radius = radius
super(Circle, self).__init__(id, color, x, y)
class Rectangle(Shape):
def __init__ (self, id, color, x, y, width, height):
self.__type = "rectangle"
self.__dim = g.Point2d(width, height)
super(Rectangle, self).__init__(id, color, x, y)
class Triangle(Shape):
def __init__ (self, id, color, x, y, bx, by, cx, cy):
self.__type = "triangle"
self.__b = g.Point2d(bx, by)
self.__c = g.Point2d(cx, cy)
super(Triangle, self).__init__(id, color, x, y)
3. Meine Frage ist, wie Formen erstellen aus dem geparsten Text? Wie kann ich den richtigen Konstruktor aufrufen und die richtige Parameterliste übergeben? Ich möchte die Verknüpfung von geparsten Parameter- und Shape-Klassen auf diese Weise übernehmen: Wenn das Programm mit einer neuen Form (z. B. Polygon) umgehen soll, möchte ich nur eine neue Klasse 'Polygon' erstellen. (z. B. ['polygon', [['id', '151'], ['color', 11403055], ['x', '10'], ['y', '10'], ['corners', '7']]]
) Was ist der pythonische Weg, dies zu tun?
Ihre Lösung funktioniert gut nach dem Umbenennen von 2 Parameter in Contructor of Triangle! – Chris