Compare commits
4 Commits
81dc543bf5
...
e18a3f062f
| Author | SHA1 | Date | |
|---|---|---|---|
| e18a3f062f | |||
| b67ebcdcca | |||
| ffc1c06a2d | |||
| 16497f059c |
32
src/tutorial/class/animal.py
Normal file
32
src/tutorial/class/animal.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
|
|
||||||
|
class Animal(ABC):
|
||||||
|
@abstractmethod
|
||||||
|
def speak(slef) -> str:
|
||||||
|
return "..."
|
||||||
|
|
||||||
|
def chorus(self, n: int) -> str:
|
||||||
|
# Wiederhole den Laut n-mal als eine Zeile"
|
||||||
|
if n <= 0:
|
||||||
|
raise ValueError("n muss positiv sein")
|
||||||
|
return " ".join(self.speak() for _ in range(n))
|
||||||
|
|
||||||
|
|
||||||
|
class Dog(Animal):
|
||||||
|
def speak(self) -> str:
|
||||||
|
return "wuff"
|
||||||
|
|
||||||
|
|
||||||
|
class Cat(Animal):
|
||||||
|
def speak(self) -> str:
|
||||||
|
return "miau"
|
||||||
|
|
||||||
|
|
||||||
|
class Cow(Animal):
|
||||||
|
def speak(self) -> str:
|
||||||
|
return "muh"
|
||||||
|
|
||||||
|
|
||||||
|
cow = Cow()
|
||||||
|
print(cow.chorus(3))
|
||||||
17
src/tutorial/class/mro_tutorial.py
Normal file
17
src/tutorial/class/mro_tutorial.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
class A:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class B(A):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class C(A):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class D(B, C):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
print(D.mro())
|
||||||
21
src/tutorial/class/robot.py
Normal file
21
src/tutorial/class/robot.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
class Dog:
|
||||||
|
def speak(self) -> str:
|
||||||
|
return "wuff"
|
||||||
|
|
||||||
|
|
||||||
|
class Cat:
|
||||||
|
def speak(self) -> str:
|
||||||
|
return "miau"
|
||||||
|
|
||||||
|
|
||||||
|
class Robot:
|
||||||
|
def speak(self) -> str:
|
||||||
|
return "bip bup"
|
||||||
|
|
||||||
|
|
||||||
|
def chorus(animals) -> list:
|
||||||
|
for a in animals:
|
||||||
|
print(a.speak())
|
||||||
|
|
||||||
|
|
||||||
|
chorus([Dog(), Cat(), Robot()])
|
||||||
27
src/tutorial/class/storage.py
Normal file
27
src/tutorial/class/storage.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
|
|
||||||
|
class Storage(ABC):
|
||||||
|
@abstractmethod
|
||||||
|
def save(self, key: str, value: str) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def load(self, key: str) -> str:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class MemoryStorage(Storage):
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self.storage = {}
|
||||||
|
|
||||||
|
def save(self, key: str, value: str) -> None:
|
||||||
|
self.storage[key] = value
|
||||||
|
|
||||||
|
def load(self, key):
|
||||||
|
return self.storage[key]
|
||||||
|
|
||||||
|
|
||||||
|
s = MemoryStorage()
|
||||||
|
s.save("randomkey", "My Value")
|
||||||
|
print(s.load("randomkey"))
|
||||||
Loading…
x
Reference in New Issue
Block a user