forked from bradtraversy/python_sandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.py
More file actions
63 lines (48 loc) · 1.84 KB
/
classes.py
File metadata and controls
63 lines (48 loc) · 1.84 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
# A class is like a blueprint for creating objects. An object has properties and methods(functions) associated with it. Almost everything in Python is an object
# Create class
class User:
# Constructor
def __init__(self, name, email, age):
self.name = name
self.email = email
self.age = age
# Adding Encapsulation of variables... Encapsulation is the concept of making the variables non-accessible or accessible upto some extent from the child classes
self._private = 1000 # Encapsulated variables are declares with '_' in the constructor.
def greeting(self):
return f'My name is {self.name} and I am {self.age}'
def has_birthday(self):
self.age += 1
#function for encap variable
def print_encap(self):
print(self._private)
# Extend class
class Customer(User):
# Constructor
def __init__(self, name, email, age):
User.__init__(self, name, email, age) #Called proper parent class constructor to make this as proper child inehriting all methods.
self.name = name
self.email = email
self.age = age
self.balance = 0
def set_balance(self, balance):
self.balance = balance
def greeting(self):
return f'My name is {self.name} and I am {self.age} and my balance is {self.balance}'
# Init user object
brad = User('Brad Traversy', 'brad@gmail.com', 37)
# Init customer object
janet = Customer('Janet Johnson', 'janet@yahoo.com', 25)
janet.set_balance(500)
print(janet.greeting())
brad.has_birthday()
print(brad.greeting())
#Encapsulation -->
brad.print_encap()
brad._private = 800 #Changing for brad
brad.print_encap()
# Method inherited from parent
janet.print_encap() #Changing the variable for brad doesn't affect janets variable --> Encapsulation
janet._private = 600
janet.print_encap()
#Similary changing janet's doesn't affect brad's variable.
brad.print_encap()