uebungen und Aufgaben 3 #4
1
src/a3_klassen2.py
Normal file
1
src/a3_klassen2.py
Normal file
@ -0,0 +1 @@
|
||||
""" """
|
||||
121
src/u3_klassen2.py
Normal file
121
src/u3_klassen2.py
Normal file
@ -0,0 +1,121 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
|
||||
class Hund(ABC):
|
||||
anzahl_hunde = 0
|
||||
|
||||
def __init__(self, name: str, alter: int, gewicht: float):
|
||||
self.name = name
|
||||
self.alter = alter
|
||||
self.gewicht = gewicht
|
||||
Hund.anzahl_hunde += 1
|
||||
|
||||
def __repr__(self):
|
||||
return (
|
||||
f"Hund(name='{self.name}', rasse='{self.rasse}',"
|
||||
f"alter={self.alter}, gewicht={self.gewicht}kg"
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.name} ist ein {self.alter}-jähriger {self.rasse}"
|
||||
|
||||
@abstractmethod
|
||||
def bellen(self, anzahl: int = 1) -> None:
|
||||
pass
|
||||
|
||||
def geburtstag(self):
|
||||
self.alter += 1
|
||||
print(
|
||||
f"Alles Gute zum Geburtstag, {self.name}! Du bist jetzt {self.alter} Jahre alt."
|
||||
)
|
||||
return
|
||||
|
||||
|
||||
class Pudel(Hund):
|
||||
def __init__(self, name: str, alter: int, gewicht: float):
|
||||
super().__init__(name, alter, gewicht)
|
||||
self.rasse = "Pudel"
|
||||
self.laut = "Wauw!"
|
||||
|
||||
def bellen(self, anzahl: int = 1) -> None:
|
||||
print(self.laut * anzahl)
|
||||
return
|
||||
|
||||
|
||||
class Labrador(Hund):
|
||||
def __init__(self, name: str, alter: int, gewicht: float):
|
||||
super().__init__(name, alter, gewicht)
|
||||
self.rasse = "Labrador"
|
||||
self.laut = "Wuff!"
|
||||
|
||||
def bellen(self, anzahl: int = 1) -> None:
|
||||
print(self.laut * anzahl)
|
||||
return
|
||||
|
||||
|
||||
class Bulldog(Hund):
|
||||
def __init___(self, name: str, alter: int, gewicht: float):
|
||||
super().__init__(name, alter, gewicht)
|
||||
self.rasse = "Bulldog"
|
||||
self.laut = "Wuffi!"
|
||||
|
||||
def bellen(self, anzahl: int = 1) -> None:
|
||||
print(self.laut * anzahl)
|
||||
return
|
||||
|
||||
|
||||
"""
|
||||
ohne abstract method
|
||||
class Hund:
|
||||
anzahl_hunde = 0
|
||||
|
||||
def __init__ (self, name: str, alter: int, gewicht: float):
|
||||
self.name = name
|
||||
self.alter = alter
|
||||
self.gewicht = gewicht
|
||||
Hund.anzahl_hunde += 1
|
||||
|
||||
def __repr__(self):
|
||||
return (
|
||||
f"Hund(name='{self.name}', rasse='{self.rasse}',"
|
||||
f"alter={self.alter}, gewicht={self.gewicht}kg"
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.name} ist ein {self.alter}-jähriger {self.rasse}"
|
||||
|
||||
def bellen(self, anzahl=1):
|
||||
if not self.laut:
|
||||
print("Wuff!" * anzahl)
|
||||
else:
|
||||
print(self.laut * anzahl)
|
||||
return
|
||||
|
||||
def geburtstag(self):
|
||||
self.alter += 1
|
||||
print(
|
||||
f"Alles Gute zum Geburtstag, {self.name}! Du bist jetzt {self.alter} Jahre alt."
|
||||
)
|
||||
return
|
||||
|
||||
|
||||
class Pudel(Hund):
|
||||
def __init__(self, name: str, alter: int, gewicht: float):
|
||||
super().__init__(name, alter, gewicht)
|
||||
self.rasse = "Pudel"
|
||||
self.laut = "Wauw!"
|
||||
|
||||
class Labrador(Hund):
|
||||
def __init__(self, name: str, alter: int, gewicht: float):
|
||||
super().__init__(name, alter, gewicht)
|
||||
self.rasse = "Labrador"
|
||||
self.laut = "Wuff!"
|
||||
"""
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
hund1 = Pudel("Bello", 1, 2)
|
||||
hund2 = Labrador("Bello", 4, 3)
|
||||
hund3 = Bulldog("Bello", 4, 3)
|
||||
|
||||
hund1.bellen(3)
|
||||
@ -1,4 +1,4 @@
|
||||
from src.moduleA import addition
|
||||
from src.u1_moduleA import addition
|
||||
|
||||
|
||||
def test_a():
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user