/ Published in: Python

Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
class DisneyCharacter(): # This variable holds the name for the class DisneyCharacter name = "" class Duck(): # This variable holds the name for the class Duck name = "" def __init__(self, name = None): """ Constructor for the class Duck """ # If the user doesn't specify a second name, "Duck" will be chosen if (name is None): name = "Duck" self.name = name def getName(self): """ Returns the name stored in the class Duck """ return self.name def __init__(self, name = None, secondName = None): """ Constructor for the class DisneyCharacter """ # If the user doesn't specify a name, "Donald" will be chosen if (name is None): name = "Donald" # Creates a new instance of Duck newDuck = DisneyCharacter.Duck(secondName) # This will join together the names from the classes DisneyCharacter and Duck self.name = name + " " + newDuck.getName() def getName(self): """ Returns the name stored in the class DisneyCharacter """ return self.name # Creates an instance of the class DisneyCharacter donald = DisneyCharacter() # Writes "Donald Duck" print(donald.getName()) # Writes "Daisy Duck" daisy = DisneyCharacter("Daisy") print(daisy.getName()) # Writes "Scrooge McDuck" unclescrooge = DisneyCharacter("Scrooge", "McDuck") print(unclescrooge.getName())
Comments
