/
tony-objandclass.py
67 lines (41 loc) · 2.98 KB
/
tony-objandclass.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
63
64
65
66
67
# A class is a template and a group of objects combine together to form a class
# class is a group of ppl with same behaviour
# Each object is called as instace variable
# Contruct an object for you - Constructors
# Encapsulation means - If you dont know what is inside the object then that means its encapsulated.
# The concept of hidding the details from a user means encapsulation
# Method is another function which resides inside the class and "self" should be used within method()
# Constructor has the same name as the name of the class
# A class name or variable can start with "_"(underscore)
#Ex : tony = BankAccount()
# print(tony.getBalance()) #Here "tony" is the "self"
# return self_balance
class BankAccount:
def __init__(self,n,b): #-----------------------
self._name=n # >>>> Constructor <<<<<
self._balance=b #-----------------------
def deposit(self,dep): #----------------------------------------------------------
self._balance=self._balance+dep # >>> Method ; This is to add additional business logic <<< Method is also know as Accessor or getter
#----------------------------------------------------------
def getBalance(self): #----------------------------------------------------------
return self._balance # >>> Method ; This is to add additional business logic <<<
#----------------------------------------------------------
def withdraw(self,w): #----------------------------------------------------------
self._balance=self._balance-w # >>> Method ; This is to add additional business logic <<<
#----------------------------------------------------------
def changeName(self,newName): #----------------------------------------------------------
self._name=newName # >>> Method ; This is to add additional business logic <<<
#----------------------------------------------------------
def setBalance(self,b): #----------------------------------------------------------
self._balance=b # >>> Method or it is also called as mutator or setter because this function allows some correction <<<
#----------------------------------------------------------
#===================================Save the below separately as tony.py==============================================
from tony-objandclass import BankAccount
tony=BankAccount("tonztoy",123456) #Here "tony" represents the address space for object and also reresent the "self"
tony.deposit(74574)
print("tonztoy has:",tony.getBalance())
tony.withdraw(23)
print ("tonztoy balance is:",tony.getBalance())
tony.changeName("preetha")
#======================================================================================================================
#we need to run the pgm by "python tony.py"