Basic OOP concepts in python
Rate this article
0 out of 5
In this tutorial we will show you how to use Basic OOP concepts in python

class Class1(object):
    
    k = 7
    
    def __init__(self, color='green'):
        
        self.color = color
    
    def Hello1(self):
        print "Hello from Class1!"
        
    def printColor(self):
  
        print "I like the color", self.color
     


class Class2(Class1):
    

    def Hello2(self):
        print "Hello from Class2!"
        print self.k, "is my favorite number"
        
    
c1 = Class1('blue')

c2 = Class2('red')

print '-'*20
print "Class1 says hello:"
c1.Hello1()

print '-'*20
print "Class2 says a Class1 hello:"
c2.Hello1()

print '-'*20
print "Class2 says its own hello:"
c2.Hello2()

print '-'*20
print "Class1 color via __init__():"
c1.printColor()


print '-'*20
print "Class2 color via inherited __init__() and printColor():"
c2.printColor()

print '-'*20
print "Class1 changes its mind about the color:"
c1 = Class1('yellow')  
c1.printColor()

print '-'*20
print "Wonder what Class2 has to say now:"
c2.printColor()




source code

   
Messages
Posted:
Post Your Comments
Name (Max 50 Chars)
Comments

Copyright © KTS Infotech 2000-2023. Site Developed Using KTS WebCloud
Reach Us on facebookWatch Video Tutorials on YouTube Channel TekTipsNavigate to KTS Technology Blog