Last active
October 8, 2015 15:38
-
-
Save j10sanders/80d0df1ad24a41f6c2ca to your computer and use it in GitHub Desktop.
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
| __author__ = 'Jonathan' | |
| class Musician(object): | |
| def __init__(self, sounds): | |
| self.sounds = sounds | |
| def solo(self, length): | |
| for i in range(length): | |
| print(self.sounds[i % len(self.sounds)],) | |
| print("") | |
| class Bassist(Musician): # The Musician class is the parent of the Bassist class | |
| def __init__(self): | |
| # Call the __init__ method of the parent class | |
| super(Bassist, self).__init__(["Twang", "Thrumb", "Bling"]) | |
| class Guitarist(Musician): | |
| def __init__(self): | |
| # Call the __init__ method of the parent class | |
| super(Guitarist, self).__init__(["Boink", "Bow", "Boom"]) | |
| def tune(self): | |
| print("Be with you in a moment") | |
| print("Twoning, sproing, splang") | |
| class Drummer(Musician): | |
| def __init__(self): | |
| # Call the __init__ method of the parent class | |
| super(Drummer, self).__init__(["Tss", "Ding", "Dong"]) | |
| def count_combust(self): | |
| print("One, two, three, four") | |
| print("Combust!") | |
| class Band(object): | |
| def __init__(self, hire): | |
| self.hire = hire | |
| def hire_person(self): | |
| if self.hire == Bassist: | |
| hire = Bassist() | |
| one = hire | |
| print(one.solo(10)) | |
| elif self.hire == Guitarist: | |
| hire = Guitarist() | |
| one = hire | |
| print(one.solo(10)) | |
| else: | |
| hire = Guitarist() | |
| print(hire.tune()) | |
| def main(): | |
| nigel = Guitarist() | |
| nigel.solo(6) | |
| nigel.tune() | |
| print("Nigel does", nigel.sounds) | |
| joe = Drummer() | |
| joe.solo(4) | |
| joe.count_combust() | |
| pythonic = Band(Guitarist) | |
| pythonic.hire_person() | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You're passing a class when you're creating the band, like
pythonic = Band(Guitarist). the problem with that is that your band will only be able to hire Guitarists! Also, it doesn't really hire them, it just invites them over for a jam, they play and then they're gone! What ifBandhad a property where you could append more band members?