Seite 1 von 1

OOP - mal wieder

Verfasst: Mittwoch 22. Juni 2011, 22:45
von zappa
Oh Mann, diese OOP macht mich noch fertig. Ich krebse so langsam vor mich hin und könnte ständig heulen :cry:

Der Code aus einem Buch:

Code: Alles auswählen


import math
class Point:
    def move(self, x, y):
        self.x = x
        self.y = y
    def reset(self):
        self.move(0, 0)
    def calculate_distance(self, other_point):
        return math.sqrt((self.x - other_point.x)**2 + (self.y - other_point.y)**2)
Wie erkläre ich mir das .x bei other_point.x?
Für other_point wird doch ein Wert übergeben - und da hänge ich jetzt das x dran???

Re: OOP - mal wieder

Verfasst: Mittwoch 22. Juni 2011, 22:48
von problembär
"other_point" ist ein Objekt, das der Methode ".calculate_distance()" bei Aufruf übergeben wird.
Dieses Objekt hat ein Atrribut x, und eines y.
Diese werden über "other_point.x" und "other_point.y" angesprochen.

"other_point" könnte sogar ein anderes Objekt der Klasse "Point" sein.

HTH

Re: OOP - mal wieder

Verfasst: Mittwoch 22. Juni 2011, 23:38
von zappa
Dankeschön... ich glaube, so langsam bekomme ich den Dreh raus (wenn auch immer mal wieder große Täler der Unwissenheit vor mir liegen :K )
Aber, die Hoffnung stirbt zuletzt...