-
Notifications
You must be signed in to change notification settings - Fork 0
/
Class_dog.py
62 lines (55 loc) · 2.49 KB
/
Class_dog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class Dog:
# This is the constructor for the class. It is called whenever a Dog
# object is created. The reference called "self" is created by Python
# and made to point to the space for the newly created object. Python
# does this automatically for us but we have to have "self" as the first
# parameter to the __init__ method (i.e. the constructor).
def __init__(self, name, month, day, year, speakText):
self.name = name
self.month = month
self.day = day
self.year = year
self.speakText = speakText
# This is an accessor method that returns the speakText stored in the
# object. Notice that "self" is a parameter. Every method has "self" as its
# first parameter. The "self" parameter is a reference to the current
# object. The current object appears on the left hand side of the dot (i.e.
# the .) when the method is called.
def speak(self):
return self.speakText
# Here is an accessor method to get the name
def getName(self):
return self.name
# This is another accessor method that uses the birthday information to
# return a string representing the date.
def birthDate(self):
return str(self.month) + "/" + str(self.day) + "/" + str(self.year)
# This is a mutator method that changes the speakText of the Dog object.
def changeBark(self,bark):
self.speakText = bark
# When creating the new puppy we don't know it's birthday. Pick the
# first dog's birthday plus one year. The speakText will be the
# concatenation of both dog's text. The dog on the left side of the +
# operator is the object referenced by the "self" parameter. The
# "otherDog" parameter is the dog on the right side of the + operator.
def __add__(self,otherDog):
return Dog("Puppy of " + self.name + " and " + otherDog.name, \
self.month, self.day, self.year + 1, \
self.speakText + otherDog.speakText)
def main():
boyDog = Dog("Mesa", 5, 15, 2004, "WOOOOF")
girlDog = Dog("Sequoia", 5, 6, 2004, "barkbark")
print(boyDog.speak())
print(girlDog.speak())
print(boyDog.birthDate())
print(girlDog.birthDate())
boyDog.changeBark("woofywoofy")
print(boyDog.speak())
puppy = boyDog + girlDog
print(puppy.speak())
print(puppy.getName())
print(puppy.birthDate())
print(str(puppy.name))
print(repr(puppy.name))
if __name__ == "__main__":
main()