Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions Ukoly/DU_9/class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
class band_member:
def __init__(self, name):
self.name = name

def play(self):
print(f'{self.name} plays on {self.instrument}')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Čo ak self.instrument nie je definované (je rovné None)? Nebolo by to dobré nejako ošetriť? Ideálny spôsob, ako napísať takúto abstraktnú triedu (to je taká trieda, že sama o sebe sa nepoužíva na instanciovanie, ale ako nejaký spoločný rodič ďalších pod-tried - ono to má svoj význam v polymorfizme), tak sa dáva na časť, ktoré závisia od rodičov vyvolanie výnimky NotImplementedError. V skratke ak neexistuje self.instrument, tak vyhodíš výnimku NotImplementedError.



class guitarist(band_member):
def play(self, number_of_strings):
self.number_of_strings = number_of_strings
if self.number_of_strings >= 4 and self.number_of_strings < 6:
self.instrument = 'bass guitar'
elif self.number_of_strings >= 6:
self.instrument = 'solo guitar'
super().play()


class singer(band_member):
def play(self):
print(f'{self.name} sings')


class drummer(band_member):
instrument = 'drums'

def __init__(self, name):
self.number_of_sticks = 2
super().__init__(name)

def throw_a_stick(self):
self.number_of_sticks += 1

def number_of_throwen_sticks(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Táto celá metóda mi nejak nedáva zmysel, okrem toho posledného výpisu na konci. V prvom kroku máš if, ktorý kontroluje, či metóda .throw_a_stick() vráti niečo, čo by sa dokázalo vyhodnotiť ako True. Avšak metóda throw_a_stick je technicky procedúra a jej návratová hodnota je vždy None. Čo je vždy False, plus ako bonus sa nám pridá jedna vyhodená palička. A nakoniec ešte pridáš ďalšie 2? To nechápem už vôbec.

if self.throw_a_stick():
self.number_of_sticks += 2
print(f'{self.name} allready threw {self.number_of_sticks} into the crowd.')


Tom = singer('Tom')

Jan = guitarist('Jan')
Jan.number_of_strings = 4

Vojta = guitarist('Vojta')
Vojta.number_of_strings = 7

Filip = guitarist('Filip')
Filip.number_of_strings = 6

Martin = drummer('Martin')
for _ in range(3):
Martin.throw_a_stick()
Martin.number_of_throwen_sticks()


guitarists = [Jan, Vojta, Filip]
members = [Martin, Tom]

for guitarist in guitarists:
guitarist.play(guitarist.number_of_strings)

for member in members:
member.play()