Repository navigation
Expand file tree
/
Copy pathModule10_Lecture_code
More file actions
114 lines (82 loc) · 3.7 KB
/
Copy pathModule10_Lecture_code
File metadata and controls
114 lines (82 loc) · 3.7 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#Module 10 - Introdction to Classes and Objects
# A Class is a blueprint for creating objects. Objects have attributes and methods. We use methods to interact with the objects.
# Code reuse, modularity, security, APIs
# Encapsulation, Abstraction, Inheritance, Polymorphism
#Object = [att1, att2, att3, method1, method2, method3]
#House Object: att1 = height, att2 = walls, att3 = foundation, att4 = doors, att5 = windows / methods = open_door(), close_door(), paint_walls()
car = "Mustang"
# Person Class
class Person:
# Constructor (the __init__ is called a dunder method)
def __init__(self, name, age):
self.name = name
self.age = age
# the self parameter is a reference to the current instance of the class, and is used to access variables that belong to the class. It does not have to be named self, you can call it whatever you want, but it is standard to use self.
# Box
# Method
def say_hello(self):
print(f"Hello, my name is {self.name} and I am {self.age} years old.")
# we can have another class in the same file. We can also create a second file that stores this class in it, and we can later call methods from the class and instantiate objects from that class.
class Dog:
#class attributes
name = ""
breed = ""
def __init__(self, name, breed):
self._name = name # _name is a private field
self._breed = breed # _breed is a private field
# Python does not support overloading constructors.
#def __init__(self):
# self._name = " "
# self._breed = " "
# The __str__ method is called when we use the print() function to print an object. It returns a string representation of the object, rather than the address associated with the class referenc variable.
def __str__(self):
return f"{self._name} is a {self._breed}."
# Accessor and mutator methods:
#setters are access methods that allow us to set the value of a private field. They are also called mutator methods.
#def set_name(self, _name):
#self.name = _name
def set_breed(self, breed):
self.breed = breed
#getters are access methods that allow us to access the value of a private field. They are also called accessor methods.
def get_name(self):
return self.name
def get_breed(self):
return self.breed
def bark(self):
print(f"{self.name} says Woof!")
def sit(self):
print(f"{self.name} sits down.")
def roll_over(self):
print(f"{self.name} rolls over.")
def shake(self):
print(f"{self.name} shakes hands.")
def lay_down(self):
print(f"{self.name} lays down.")
def fetch(self):
print(f"{self.name} fetches the ball.")
def speak(self):
print(f"{self.name} speaks.")
if __name__ == "__main__":
# Create an object of the Person class
person1 = Person("Alice", 25) #instantiate the object
person1.say_hello()
print(person1.name)
person2 = Person("Bob", 30)
person2.say_hello()
print(person2.name)
person3 = Person("Charlie", 35)
person3.say_hello()
person4 = Person("David", 40)
person4.say_hello()
dog0 = Dog("Buddy", "Cocker Spaniel")
print(dog0) # this will print the address of the object in memory. We can overload the __str__ method to print the name and breed of the dog.
#print(str(dog0))
dog0.bark()
dog0.sit()
# dog1 = Dog() # this would throw an error if we did not have the overloaded constructor above.
dog0 = Dog("Fido" , "olden")
print(dog0)
#og0.set_name("Fido")
dog0.set_breed("Golden Retriever")
print(dog0.get_name())
print(dog0.get_breed())