article

Sunday, May 29, 2016

Python OOP object oriented programming Example code

Python OOP object oriented programming Example code
 
#OOP object oriented programming
#class keyword
class First:
   pass
fr = First()
print type(fr)
print type(First)
#---------------------------------------------------------------------
class Cat:
   def __init__(self, name):
      self.name = name
missy = Cat('Missy')
lucky = Cat('Lucky')
print missy.name # Missy
print lucky.name # Lucky
#---------------------------------------------------------------------
class Cat:
   species = 'mammal'
   def __init__(self, name, age):
      self.name = name
      self.age = age
missy = Cat('Missy', 3)
lucky = Cat('Lucky', 5)
print missy.name, missy.age # Missy 3
print lucky.name, lucky.age # Lucky 5
print Cat.species # mammal
print missy.__class__.species # mammal
print lucky.__class__.species # mammal
#---------------------------------------------------------------------
#Methods
#Methods are functions defined inside the body of a class
class Circle:
   pi = 3.141592
   def __init__(self, radius=1):
      self.radius = radius 
   def area(self):
      return self.radius * self.radius * Circle.pi
   def setRadius(self, radius):
      self.radius = radius
   def getRadius(self):
      return self.radius
c = Circle()
c.setRadius(5)
print c.getRadius() # 5
print c.area() # 78.5398
#---------------------------------------------------------------------
class Methods:
   def __init__(self):
      self.name = 'Methods'
   def getName(self):
      return self.name
m = Methods()
print m.getName() # Methods
print Methods.getName(m) # Methods
#---------------------------------------------------------------------
#Inheritance
#inheritance is a way to form new classes using classes that have already been defined
class Animal:
   def __init__(self):
      print "Animal created"
   def whoAmI(self):
      print "Animal"
   def eat(self):
      print "Eating"
class Dog(Animal):
   def __init__(self):
      Animal.__init__(self)
      print "Dog created"
   def whoAmI(self):
      print "Dog"
   def bark(self):
      print "Woof!"
d = Dog()
d.whoAmI()
d.eat()
d.bark()
#---------------------------------------------------------------------
class Animal:
   def __init__(self, name=''):
      self.name = name
   def talk(self):
      pass
class Cat(Animal):
   def talk(self):
      print "Meow!"
class Dog(Animal):
   def talk(self):
      print "Woof!"
a = Animal()
a.talk()
c = Cat("Missy")
c.talk()
d = Dog("Rocky")
d.talk()
# Meow! Woof!
#---------------------------------------------------------------------
#Special Methods name
class Book:
   def __init__(self, title, author, pages):
      print "A book is created"
      self.title = title
      self.author = author
      self.pages = pages
   def __str__(self):
      return "Title:%s , author:%s, pages:%s " % \
              (self.title, self.author, self.pages)
   def __len__(self):
      return self.pages
   def __del__(self):
      print "A book is destroyed"
book = Book("Inside Steve's Brain", "Leander Kahney", 304)
print book
print len(book)
del book

Related Post