Skip to content
Snippets Groups Projects
Commit 3d0b9d38 authored by Josef Wachtler's avatar Josef Wachtler
Browse files

add code examples

parent e01909b4
No related branches found
No related tags found
No related merge requests found
Showing with 726 additions and 89 deletions
.idea/
*.pyc
# Copyright (c) 2023 Daniel Dohr, Josef Wachtler
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Da die Klassen recht klein sind, ist es übersichtlicher sie in einer Datei zusammenzufassen.
# Die einfache Version der Bus Klasse.
class Bus:
typ = "undefined"
driver = "undefined"
number_of_seats = 0
def __init__(self, typ, number_of_seats, driver):
self.typ = typ
self.number_of_seats = number_of_seats
self.driver = driver
# Ausgabe der Attribute
def printData(self):
print("\t\tName: {}\t Bustyp: {}\t Anzahl der Sitze: {}".format(self.driver, self.typ, self.number_of_seats))
# Klasse mit public Attributen
class BusTypA:
typ = "17b"
number_of_seats = 41
driver = "undefined"
def __init__(self, driver):
self.driver = driver
def printData(self):
print("\t\tName: {}\t Bustyp: {}\t Anzahl der Sitze: {}".format(self.driver, self.typ, self.number_of_seats))
# Klasse mit einem Konstruktor der einen optionalen Parameter enthält
class BusTypB:
typ = "17b"
number_of_seats = 41
driver = "undefined"
def __init__(self, typ, driver="Fahrer1"):
self.driver = driver
self.typ = typ
def printData(self):
print("\t\tName: {}\t Bustyp: {}\t Anzahl der Sitze: {}".format(self.driver, self.typ, self.number_of_seats))
# Copyright (c) 2023 Daniel Dohr, Josef Wachtler
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Klasse mit einem privaten Attribut
class Dog:
name = "undefined"
# Underscores werden verwendet um ein Attribut als privat zu markieren
def __init__(self, name):
self.name = name
self.__private_variable = "Bello ist versteckt."
def printData(self):
print("\t\tWurde {} gefunden? {}".format(self.name, self.__private_variable))
# Copyright (c) 2023 Daniel Dohr, Josef Wachtler
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Import Statements um die Inhalte der anderen Dateien hier zugänglich zu machen.
import sys
import os
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
from Buses import Bus, BusTypA, BusTypB
from Dog import Dog
# Diese Methode wird in main.py ausgeführt und soll die Ergebnisse in einer leserlichen Form ausgeben.
def run_lecture_two():
print("\n*** Lecture 2 Start ***")
print(" --Ausgabe der Bus Beispiele-- \n")
text_wrapper("Erstes Beispiel", first_bus_example)
text_wrapper("Zweites Beispiel", second_bus_example)
text_wrapper("Drittes Beispiel", third_bus_example)
print(" --Ausgabe des Dog Beispiels-- \n")
text_wrapper("Dog Beispiel", dog_example)
print("*** Lecture 2 End ***")
# Um eine gleichmässige Ausgabe zu gewährleisten und Code zu sparen haben wir hier eine kleine Hilfsfunktion
def text_wrapper(title, example):
print(f"\t - {title} -")
example()
print("\n")
# Das erste Bus-Beispiel das mit der Klasse Bus arbeitet
def first_bus_example():
bus1 = Bus("3a", 38, "John A.")
bus2 = Bus("17b", 42, "Klaus M.")
bus1.printData()
bus2.printData()
# Das zweite Bus-Beispiel, bei dem wir sehen, dass wir Attribute verändern können
def second_bus_example():
bus1 = BusTypA("John A.")
bus2 = BusTypA("Klaus M.")
bus1.typ = "3a"
bus1.printData()
bus2.printData()
# Das dritte Bus-Beispiel das die Verwendung von optionalen Parametern zeigt
def third_bus_example():
bus1 = BusTypB("3a")
bus2 = BusTypB("2a", "Klaus M.")
bus1.printData()
bus2.printData()
# Dieses Beispiel zeigt das von außen auch auf private Attribute zugegriffen werden kann
def dog_example():
bello = Dog("Bello")
bello.printData()
bello.__private_variable = "\tAuf der Suche nach Bello"
bello.printData()
bello._Dog__private_variable = "Bello wurde gefunden"
bello.printData()
# Dieses File wird verwendet, um ein Paket in Python zu erstellen.
# Copyright (c) 2023 Daniel Dohr, Josef Wachtler
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# The basic dog version with simple getters and setters
class BasicDog:
__name = ""
__age = 0
def __init__(self, name, age):
self.__name = name
self.__age = age
def get_name(self):
return self.__name
def get_age(self):
return self.__age
def set_age(self, newAge):
self.__age = newAge
# Copyright (c) 2023 Daniel Dohr, Josef Wachtler
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Die Calculator Klasse soll die Verwendung von statischen Methoden zeigen
class Calculator:
# @ signalisiert die Verwendung eines Decorators. Statische Methoden verwenden keine objektbezogenen Informationen
@staticmethod
def addNumbers(integerlist):
value = 0
for number in integerlist:
value += number
return value
# Copyright (c) 2023 Daniel Dohr, Josef Wachtler
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Diese Klasse enthält alle Methodenversionen.
class DecoratedDog:
name = "undefined"
species = "Canis"
def __init__(self, name):
self.name = name
# Beispiel einer Instanz Methode
def bark_in_language(self, language):
match language:
case "french":
print("\t\t{}: wau, wau".format(self.name))
case "englisch":
print("\t\t{}: woof, woof".format(self.name))
case _:
print("\t\t{}: barf, barf".format(self.name))
# @ signalisiert die Verwendung eines Decorators.
@classmethod
def print_species(cls):
print(("\t\t{}'s spezies ist: " + cls.species).format(cls.name))
@staticmethod
def bark():
print("\t\tWoof, woof")
# Copyright (c) 2023 Daniel Dohr, Josef Wachtler
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Das ist unser selbst erstellter Decorator. Eine Funktion wird als Parameter übergeben
def our_decorator(function_we_want_to_modify):
# In ihm wird eine weitere Funktion definiert die, die übergebene Funktion umschließt
def wrapper():
print("\t\tDie Funktion wird um diese Ausgabe erweitert: 1/2")
function_we_want_to_modify()
print("\t\tDie Funktion wird um diese Ausgabe erweitert: 2/2 \n")
return wrapper
# wir können mit @ einer Funktion einen Decorator zuweisen.
@our_decorator
def decorated_function():
print("\t\tMeine Dekoration wird über das @-Zeichen bestimmt.")
# Oder wir definieren wie sonst auch unsere Funktion und rufen die Decorator Funktion auf.
def unsuspecting_function():
print("\t\tMeine Dekoration wird direkt zugewiesen.")
# Das Resultat ist dasselbe
def decorator_example():
function_handle = our_decorator(unsuspecting_function)
function_handle()
decorated_function()
# Copyright (c) 2023 Daniel Dohr, Josef Wachtler
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Import Statements um die Inhalte der anderen Dateien hier zugänglich zu machen.
import sys
import os
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
from BasicDog import BasicDog
from DecoratedDog import DecoratedDog
from Calculator import Calculator
from DecoratorExample import decorator_example
# Diese Methode wird in main.py ausgeführt und soll die Ergebnisse in einer leserlichen Form ausgeben.
def run_lecture_three():
print("\n*** Lecture 3 Start ***")
print(" --Verwendung der verschiedenen Methodenarten-- \n")
text_wrapper("Einfacher Zugriff auf ein Objektattribut", basic_attribute_output_example)
text_wrapper("Aufruf einer Instanz Methode", instance_method_example)
text_wrapper("Verwendung einer Klassenmethode", class_method_example)
text_wrapper("Verwendung einer statischen Methode", static_method_example)
print(" --Ausgabe des Decorator Beispiels-- \n")
text_wrapper("Decorator Beispiel", decorator_example)
print("*** Lecture 3 End ***")
# Um eine gleichmässige Ausgabe zu gewährleisten und Code zu sparen haben wir hier eine kleine Hilfsfunktion
def text_wrapper(title, example):
print(f"\t - {title} -")
example()
print("\n")
# Das erste Dog Beispiel
def basic_attribute_output_example():
dog1 = BasicDog("Zeus", 3)
print("\t\t Output: {}".format(dog1.get_name()))
# Beispiel einer einfachen Instanz Methode
def instance_method_example():
dog1 = DecoratedDog("Bello")
dog1.bark_in_language("french")
DecoratedDog.bark()
# Beispiel einer Klassenmethode
def class_method_example():
dog1 = DecoratedDog("Bello")
DecoratedDog.print_species() # Print über Klasse
dog1.print_species() # Print über Referenz
dog1.species = "Katze"
print("\t\t{}".format(dog1.species))
dog1.print_species()
# Beispiel einer statischen Methode
def static_method_example():
calc = Calculator()
print("\t\t{}".format(Calculator.addNumbers({2, 4, 6})))
print("\t\t{}".format(calc.addNumbers({2, 4, 6})))
# Dieses File wird verwendet, um ein Paket in Python zu erstellen.
# Copyright (c) 2023 Daniel Dohr, Josef Wachtler
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Von dieser Klasse werden die anderen Tiere erben.
class Animal:
__name = ""
__age = 0
def __init__(self, name, age):
self.__name = name
self.__age = age
def get_name(self):
return self.__name
def get_age(self):
return self.__age
def eat(self):
print("\t\t{} is eating.".format(type(self).__name__))
def say_hello(self):
print("\t\tAnimal noises")
# Copyright (c) 2023 Daniel Dohr, Josef Wachtler
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from Animal import Animal
class Beaver(Animal):
def __init__(self, name, age):
super().__init__(name, age)
def knock(self):
print("\t\tKnock, knock.")
def say_hello(self):
print("\t\tgrunt, grunt")
# Copyright (c) 2023 Daniel Dohr, Josef Wachtler
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from Animal import Animal
class Duck(Animal):
def __init__(self, name, age):
super().__init__(name, age)
def swim(self):
print("\t\tThis animal can swim")
def say_hello(self):
print("\t\tquack quack")
# Copyright (c) 2023 Daniel Dohr, Josef Wachtler
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from Animal import Animal
class InheritanceDog(Animal):
def __init__(self, name, age):
super().__init__(name, age)
def say_hello(self):
print("\t\twoof woof")
# Copyright (c) 2023 Daniel Dohr, Josef Wachtler
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from Duck import Duck
from Beaver import Beaver
# Mehrfachvererbung um die Methoden von Duck und Beaver zu erhalten
class Platypus(Duck, Beaver):
def __init__(self, name, age):
super().__init__(name, age)
def say_hello(self):
# Wir müssen die Klasse übergeben, von der wir nicht verwenden wollen
return super(Duck, self).say_hello()
# Copyright (c) 2023 Daniel Dohr, Josef Wachtler
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Import Statements um die Inhalte der anderen Dateien hier zugänglich zu machen.
import sys
import os
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
from Duck import Duck
from InheritanceDog import InheritanceDog
from Beaver import Beaver
from Platypus import Platypus
# Diese Methode wird in main.py ausgeführt und soll die Ergebnisse in einer leserlichen Form ausgeben.
def run_lecture_four():
print("\n*** Lecture 4 Start ***")
print(" --Verwendung von einfacher Vererbung-- \n")
text_wrapper("Ausgabe des Vererbungs-Beispiels", basic_inheritance_example)
print(" --Verwendung von mehrfach Vererbung-- \n")
text_wrapper("Ausgabe des Mehrfachvererbungs-Beispiels", multi_inheritance_example)
print("*** Lecture 4 End ***")
# Um eine gleichmässige Ausgabe zu gewährleisten und Code zu sparen haben wir hier eine kleine Hilfsfunktion
def text_wrapper(title, example):
print(f"\t - {title} -")
example()
print("\n")
# Das erste Vererbungsbeispiel. Wir sehen, dass die Methoden aus der Animal Klasse verwendet werden können.
def basic_inheritance_example():
hund1 = InheritanceDog("Zeus", 2)
hund1.say_hello()
ente1 = Duck("Friedolin", 4)
ente1.eat()
ente1.say_hello()
bieber = Beaver("Günther", 3)
bieber.say_hello()
# Unser Schnabeltier kann Methoden aus der Duck und Beaver Klasse verwenden.
def multi_inheritance_example():
schnabel = Platypus("Hercules", 2)
schnabel.swim()
schnabel.knock()
schnabel.say_hello()
# Dieses File wird verwendet, um ein Paket in Python zu erstellen.
# Programmieren lernen mit Python - OOP # Programmieren lernen mit Python - Objektorientierte Programmierung
Dieses Projekt enthält die Beispiele aus dem iMooX.at Kurs
"Programmieren Lernen mit Python: Objektorientierte Programmierung".
Zu jeder Lektion finden Sie ein Paket das den Lektionscode und Helfermethoden enthält.
## Ausführen
## Getting started Um die Inhalte auszugeben, führen Sie das main.py Script aus. Nutzen Sie dafür entweder
die Eingabeaufforderung -> dir/to/file> python3 main.py
To make it easy for you to get started with GitLab, here's a list of recommended next steps. oder eine IDE ihrer Wahl.
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
## Add your files
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
```
cd existing_repo
git remote add origin https://gitlab.tugraz.at/llt_public/learninglab_coop/programmieren-lernen-mit-python/python-oop.git
git branch -M main
git push -uf origin main
```
## Integrate with your tools
- [ ] [Set up project integrations](https://gitlab.tugraz.at/llt_public/learninglab_coop/programmieren-lernen-mit-python/python-oop/-/settings/integrations)
## Collaborate with your team
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
## Test and Deploy
Use the built-in continuous integration in GitLab.
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
***
# Editing this README
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template.
## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
## Name
Choose a self-explaining name for your project.
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
## License
For open source projects, say how it is licensed.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
main.py 0 → 100644
# Copyright (c) 2023 Daniel Dohr, Josef Wachtler
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Hier werden die Packages importiert die unsere Beispiele beinhalten
from Lektion2.RunLecture2 import run_lecture_two
from Lektion3.RunLecture3 import run_lecture_three
from Lektion4.RunLecture4 import run_lecture_four
# Wenn wir unser Script starten wird der Code von allen 3 Lektionen ausgeführt und ausgegeben.
# Wenn nur der Code von einer Lektion ausgegeben werden soll, die anderen einfach auskommentieren.
if __name__ == '__main__':
run_lecture_two()
run_lecture_three()
run_lecture_four()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment