Вы находитесь на странице: 1из 3

OOP

   Terminology:  

• Class  −  A  user-­‐defined  prototype/blueprint/template  for  an  object  that  defines  a  set  


of   attributes   and   methods   that   characterize   any   object   of   the   class.   The   attributes  
are  data  members  (class  variables  and  instance  variables)  and  methods  is  accessed  
via  dot  notation.  
• Class   variable  −   A   variable   that   is   shared   by   all   instances   of   a   class.   Class   variables  
are   defined   within   a   class   but   outside   any   of   the   class's   methods.   Class   variables   are  
not  used  as  frequently  as  instance  variables  are.  
• Data   member  −   A   class   variable   or   instance   variable   holds   data   associated   with   a  
class  and  its  objects.  
• Function   overloading  −   The   assignment   of   more   than   one   behavior   to   a   particular  
function.   The   operation   performed   varies   by   the   types   of   objects   or   arguments  
involved.  
• Instance   variable  −   A   variable   that   is   defined   and   belongs   only   to   the   current  
instance  of  a  class.  
• Inheritance  −   The   transfer   of   the   characteristics   of   a   class   to   other   classes   that   are  
derived  from  it.  
• Instance  −   An   individual   object   of   a   certain   class.   An   object   that   belongs   to   a   class  
Circle,  for  example,  is  an  instance  of  the  class  Circle.  
• Instantiation  −  The  creation  of  an  instance  of  a  class.  
• Method  −  A  function  that  is  defined  in  a  class  definition.  
• Object  −   A   unique   instance   of   a   data   structure   that's   defined   by   its   class.   An   object  
comprises  both  data  members  (class  variables  and  instance  variables)  and  methods.  
• Operator   overloading  −   The   assignment   of   more   than   one   method   to   a   particular  
operator.  
• Constructor  –  a  special  method  called  __init__    that    set  the  initial  value  of  the  object  
variable  when  you  create  the  object      (instance  of  class)  
 
Consider  the  following  code:  

#  lab2a.py  
def      kinetic_energy(mass,velocity):  
               value  =  0.5  *  mass  *  velocity  **  2    
               return(value)  
 
mass=input("Enter  value  of  mass:  ")  
mass  =float(mass)  
 
mass=input("Enter  value  of  velocity:")  
mass  =float(velocity)  
 
result  =    kinetic_energy(mass,velocity)  
print("The  value  of  kinetic  energy    is  ",  result)  
 
m1=9.8  
v  1=  10.00  
print("The  value  of  kinetic  energy    is  ",  kinetic_energy(m1,v1)  
 
m2  =  100  
v2  =  909.0  
print(“The  value  of  kinetic  energy    is  “,  kinetic_energy(m2,v2))  
 
How  do  we  implement  the  above  code  to    become    OOP  program  
1. Identify  the  variable  and  function  in  the  program.  (variable  –  attribute,  function  -­‐  
method)  
2. Create  a  class  say  call  Object  
3. Add  a  constructor  method  called    __init__  (self,mass,velocity)  and  method  that  you  
identify  in  step  1  
4. Create  an  instance  of  the  class  –  object  variable  
5. Called  the  object  method  
Our    OOP  program    will  look  as  follows:  

#  lab2b.py  
class    Object:  

         def    __init__(self,mass,velocity):  
                       self.mass  =  mass  
                       self.velocity  =  velocity  
 
         def      kinetic_energy(self):  
                         return(0.5*self.mass*self.velocity**2)  
 

mass=float(input("Enter  value  of  mass:  "))  


velocity=float(input("Enter  value  of  velocity:"))  
object  =  Object(mass,velocity)  
print(  “  The  kinetic  energy  of  the  object  is”  ,  object.kinetic_energy())  
m1=9.8  
v  1=  10.00  
object1  =  Object(m1,v1)  
print(  “  The  kinetic  energy  of  the  object  is”  ,  object1.kinetic_energy())  
 
m2  =  100  
v2  =  909.0  
object2  =  Object(m2,v2)  
print(  “  The  kinetic  energy  of  the  object  is”  ,  object2.kinetic_energy())  
 

NB      Class  is  also  called  a  template  ,  blueprint  or  prototype    


 

Q1.    Add  and  an  additional  method  called  potential_energy  in  class  Object  where  the  formula  
is        

 mass  *  gravity  *  height  


Save  the  file  as  lab2c.py  

(You  need  to  modify  the  constructor  to  include  the  two  parameter  gravity  and  height.)  
Q2.  Add  an  additional  method  called  total_energy    where  the  formula  is:  
                     self.kinetic_energy()  +  self.potential_energy()  

Save  the  file  as  lab2d.py  


Q3.    Create  a  OOP  program  with  a  class  called  Centigrade  ,  a  constructor  method  and  method  
called    to_farenheit      where  the  formula  to  convert  the  centigrade  to  farenheit  is  as  follows:  

 farenheit = centigrade × 1.8 + 32

 Save  the  file  as    lab2e.py  

Q4.    Identify  a  formula  and  variable  that  you  can  turn  into  a  class  with  attribute  and  method.  
Create  an  instance  of  the  class,  and  then  called  the  object  method.  Save  the  file  as  lab2f.py  
 

Вам также может понравиться