Created
May 10, 2019 09:15
-
-
Save vasekch/885c4e8fb70f1736bc6fd003c7e6c26b to your computer and use it in GitHub Desktop.
PyLadies jaro 2019 NTK - Tridy I. - Kotatka
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Pouzijeme tridu kotatko z modulu (souboru) zviratka.py | |
''' | |
from zviratka import Kotatko | |
k1 = Kotatko("Micka") | |
k1.uber_zivot() | |
k2 = Kotatko("Mourek") | |
k1.uber_zivot() | |
k2.snez("granulky") | |
k1.uber_zivot() | |
k1.snez("ryba") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Pouzijeme tridu kotatko z modulu (souboru) zviratka.py | |
''' | |
from zviratka import Kotatko | |
# verze pred pridanim povinneho jmeno v konstruktoru | |
# k1 = Kotatko() | |
k1 = Kotatko("Micka") | |
k1.zamnoukej() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Vytvorme tridu Kotatko. | |
Objekty (instance) teto tridy musi mit atribut "jmeno", ten bude vyzadovany konstruktorem | |
Kazdy objekt se "narodi" s 9 zivoty. | |
Pretizime metodu __str__ (stringova reprezentace), aby se pri pouziti objektu v print() | |
vypsalo jmeno kotatka, misto adresy v pameti. | |
Trida bude definovat metody: | |
- zamnoukej() - print mnau | |
- uber_zivot() - zivoty -1 | |
- snez(jidlo) - zivoty +1, print mnam zprava | |
''' | |
class Kotatko: | |
def zamnoukej(self): | |
print("Mnaaau!") | |
def __init__(self, jmeno_str): | |
self.jmeno = jmeno_str | |
self.zivoty = 9 | |
print("Jsem nove kotatko jmenem {}".format(self.jmeno)) | |
def __str__(self): | |
return(self.jmeno) | |
def uber_zivot(self): | |
self.zivoty -= 1 | |
print("{}: Au, uz mam jen {} zivot".format(self.jmeno, self.zivoty)) | |
def snez(self, jidlo): | |
print("{}: Mnam, {} je dobre.".format(self.jmeno, jidlo)) | |
self.zivoty += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment