#define a class
class User :
def __init__(self,name,user_id,acc_balance):
self.Name=name
self.User_id=user_id
self.Acc_Balance=acc_balance
def recharge(self,amount):
self.Acc_Balance+=amount
def withdraw(self,amount):
if amount <= self.Acc_Balance :
self.Acc_Balance-=amount
else :
pass #do nothing
def getBalance(self): #parameter less method
return self.Acc_Balance
def __del__(self) :
print("Destructor started!")
#end of the class
usr=User("Kanishka",1133,27000.00)
print("User {0} , Balance {1:5.2f}".format(usr.Name,usr.getBalance()))
usr.withdraw(2000.50) #withdrwaw money
print("User {0} , Balance {1:5.2f}".format(usr.Name,usr.getBalance()))
Showing posts with label self. Show all posts
Showing posts with label self. Show all posts
Saturday, January 21, 2012
Python Note 6(Classes and Objects)
Sample Code : General class
Labels:
Classes,
constructor,
del,
destructor,
encapsulation,
functions,
Inheritance,
init,
Objects,
private,
protected,
public,
Python,
self
Subscribe to:
Posts (Atom)