Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
| modul:m320_2024:learningunits:lu09:zweiseitige_mehrfachbeziehung [2024/09/23 09:12] – angelegt msuter | modul:m320_2024:learningunits:lu09:zweiseitige_mehrfachbeziehung [2024/09/23 09:48] (aktuell) – [Umsetzung in Python] msuter | ||
|---|---|---|---|
| Zeile 19: | Zeile 19: | ||
| Nur wenn dies der Fall ist, wird die Beziehung hinzugefügt bzw. gelöscht. | Nur wenn dies der Fall ist, wird die Beziehung hinzugefügt bzw. gelöscht. | ||
| - | ==== Beziehungen hinzufügen (**add**)==== | ||
| <code python> | <code python> | ||
| + | class HolidayDestination: | ||
| + | def __init__(self, | ||
| + | self._location = location | ||
| + | self._people = [] | ||
| + | |||
| + | def __str__(self): | ||
| + | output = f' | ||
| + | for person in self._people: | ||
| + | output += f' {person._name},' | ||
| + | output += ' | ||
| + | return output | ||
| + | |||
| + | def add_person(self, | ||
| + | if not person in self._people: | ||
| + | self._people.append(person) | ||
| + | person.add_destination(self) | ||
| + | |||
| + | def remove_person(self, | ||
| + | if person in self._people: | ||
| + | self._people.remove(person) | ||
| + | person.remove_destination(self) | ||
| + | |||
| + | |||
| + | class Person: | ||
| + | def __init__(self, | ||
| + | self._name = name | ||
| + | self._destinations = [] | ||
| + | |||
| + | def __str__(self): | ||
| + | output = f' | ||
| + | for destination in self._destinations: | ||
| + | output += f' {destination._location},' | ||
| + | output += ' | ||
| + | return output | ||
| + | |||
| + | def add_destination(self, | ||
| + | if not destination in self._destinations: | ||
| + | self._destinations.append(destination) | ||
| + | destination.add_person(self) | ||
| + | |||
| + | def remove_destination(self, | ||
| + | if destination in self._destinations: | ||
| + | self._destinations.remove(destination) | ||
| + | destination.remove_person(self) | ||
| + | |||
| + | |||
| + | def main(): | ||
| + | # let's start by creating some people and destinations | ||
| + | maya = Person(' | ||
| + | thomas = Person(' | ||
| + | jusuf = Person(' | ||
| + | valley = HolidayDestination(' | ||
| + | atlantis = HolidayDestination(' | ||
| + | |||
| + | # now we let people visit the destinations | ||
| + | maya.add_destination(valley) | ||
| + | valley.add_person(thomas) | ||
| + | print(valley) | ||
| + | atlantis.add_person(maya) | ||
| + | jusuf.add_destination(atlantis) | ||
| + | print(atlantis) | ||
| + | print(maya, thomas, jusuf) | ||
| + | |||
| + | # try to add two duplicates | ||
| + | jusuf.add_destination(atlantis) | ||
| + | atlantis.add_person(maya) | ||
| + | print(atlantis) | ||
| + | |||
| + | # and now some people are cancelling their vacation | ||
| + | valley.remove_person(thomas) | ||
| + | print (valley) | ||
| + | jusuf.remove_destination(atlantis) | ||
| + | print (jusuf) | ||
| + | |||
| + | # finally we try to remove non-existing relationships | ||
| + | atlantis.remove_person(thomas) | ||
| + | print(atlantis) | ||
| + | jusuf.remove_destination(valley) | ||
| + | print (jusuf) | ||
| + | |||
| + | if __name__ == ' | ||
| + | main() | ||
| </ | </ | ||
| <WRAP center round tip 60%> | <WRAP center round tip 60%> | ||
| - | Je nach Entwurf der Applikation können | + | Natürlich könnte man die Methoden auch so anpassen, dass sie als Parameter |
| </ | </ | ||