Return to Snippet

Revision: 27877
at June 25, 2010 18:26 by Cesco


Updated Code
#!/usr/bin/env python

class man(object):
    
    # name of the man
    name = ""
    
    def __init__(self, P_name):
        """ Class constructor """
        self.name = P_name
        print("Here comes " + self.name)
        
    def talk(self, P_message):        
        print(self.name + " says: '" + P_message + "'")        
        
    def walk(self):
        """ This let an instance of a man to walk """
        print(self.name + " walks")

# This class inherits from Man class
# A superman has all the powers of a man (A.K.A. Methods and Properties in our case ;-)
class superman(man):
    
    # Name of his secret identity
    secret_identity = ""
    
    def __init__(self, P_name, P_secret_identity):
        """ Class constructor that overrides its parent class constructor"""
        # Invokes the class constructor of the parent class #
        super(superman, self).__init__(P_name)
        # Now let's add a secret identity
        self.secret_identity = P_secret_identity
        print("...but his secret identity is '" + self.secret_identity + "' and he's a super-hero!")
    
    def walk(self, P_super_speed = False):
        # Overrides the normal walk, because a superman can walk at a normal
        # pace or run at the speed of light!
        if (not P_super_speed): super(superman, self).walk()
        else: print(self.secret_identity + " run at the speed of light")
        
    def fly(self):
        """ This let an instance of a superman to fly """
        # No man can do this!
        print(self.secret_identity + " fly up in the sky")
        
    def x_ray(self):
        """ This let an instance of a superman to use his x-ray vision """
        # No man can do this!
        print(self.secret_identity + " uses his x-ray vision")
        

# Declare some instances of man and superman
lois = man("Lois Lane")
jimmy = man("Jimmy Olsen")
clark = superman("Clark Kent", "Superman")

# Let's puth them into action!

print("\n--> Let's see what a man can do:\n")
jimmy.walk()
lois.talk("Oh no, we're in danger!")

print("\n--> Let's see what a superman can do:\n")
clark.walk()
clark.talk("This is a job for SUPERMAN!")
clark.walk(True)
clark.fly()
clark.x_ray()

Revision: 27876
at June 25, 2010 18:25 by Cesco


Updated Code
#!/usr/bin/env python

class man(object):
    
    # name of the man
    name = ""
    
    def __init__(self, P_name):
        """ Class constructor """
        self.name = P_name
        print("Here comes " + self.name)
        
    def talk(self, P_message):        
        print(self.name + " says: '" + P_message + "'")        
        
    def walk(self):
        """ This let an instance of a man to walk """
        print(self.name + " walks")

# This class inherits from Man class
# A superman has all the powers of a man (A.K.A. Methods and Properties in our case ;-)
class superman(man):
    
    # Name of his secret identity
    secret_identity = ""
    
    def __init__(self, P_name, P_secret_identity):
        """ Class constructor that overrides its parent class constructor"""
        # Invokes the class constructor of the parent class #
        super(superman, self).__init__(P_name)
        # Now let's add a secret identity
        self.secret_identity = P_secret_identity
        print("...but his secret identity is '" + self.secret_identity + "' and he's a super-hero!")
    
    def walk(self, P_super_speed = False):
        # Overrides the normal walk, because a superman can walk at a normal
        # pace or run at the speed of light!
        if (not P_super_speed): super(superman, self).walk()
        else: print(self.secret_identity + " run at the speed of light")
        
    def fly(self):
        """ This let an instance of a superman to fly """
        # No man can do this!
        print(self.secret_identity + " fly up in the sky")
        
    def x_ray(self):
        """ This let an instance of a superman to use his x-ray vision """
        # No man can do this!
        print(self.secret_identity + " uses his x-ray vision")
        

# Declare some instances of man and superman
lois = man("Lois Lane")
jimmy = man("Jimmy Olsen")
clark = superman("Clark Kent", "Superman")

# Let's puth them into action!

print("\n*** Let's see what a man can do:\n")
jimmy.walk()
lois.talk("Oh no, we're in danger!")

print("\n*** Let's see what a superman can do:\n")
clark.walk()
clark.talk("This is a job for SUPERMAN!")
clark.walk(True)
clark.fly()
clark.x_ray()

Revision: 27875
at June 25, 2010 18:24 by Cesco


Updated Code
#!/usr/bin/env python

class man(object):
    
    # name of the man
    name = ""
    
    def __init__(self, P_name):
        """ Class constructor """
        self.name = P_name
        print("Here comes " + self.name)
        
    def talk(self, P_message):        
        print(self.name + " says: '" + P_message + "'")        
        
    def walk(self):
        """ This let an instance of a man to walk """
        print(self.name + " walks")

# This class inherits from Man class
# A superman has all the powers of a man (A.K.A. Methods and Properties in our case ;-)
class superman(man):
    
    # Name of his secret identity
    secret_identity = ""
    
    def __init__(self, P_name, P_secret_identity):
        """ Class constructor that overrides its parent class constructor"""
        # Invokes the class constructor of the parent class #
        super(superman, self).__init__(P_name)
        # Now let's add a secret identity
        self.secret_identity = P_secret_identity
        print("...but his secret identity is '" + self.secret_identity + "' and he's a super-hero!")
    
    def walk(self, P_super_speed = False):
        # Overrides the normal walk, because a superman can walk at a normal
        # pace or run at the speed of light!
        if (not P_super_speed): super(superman, self).walk()
        else: print(self.secret_identity + " run at the speed of light")
        
    def fly(self):
        """ This let an instance of a superman to fly """
        # No man can do this!
        print(self.secret_identity + " fly up in the sky")
        
    def x_ray(self):
        """ This let an instance of a superman to use his x-ray vision """
        # No man can do this!
        print(self.secret_identity + " uses his x-ray vision")
        

# Declare some instances of man and superman
lois = man("Lois Lane")
jimmy = man("Jimmy Olsen")
clark = superman("Clark Kent", "Superman")

# Let's puth them into action!

print("\nLet's see what a man can do:\n")
jimmy.walk()
lois.talk("Oh no, we're in danger!")

print("\nLet's see what a superman can do:\n")
clark.walk()
clark.talk("This is a job for SUPERMAN!")
clark.walk(True)
clark.fly()
clark.x_ray()

Revision: 27874
at June 25, 2010 18:07 by Cesco


Initial Code
#!/usr/bin/env python

class man(object):
    
    # name of the man
    name = ""
    
    def __init__(self, P_name):
        """ Class constructor """
        self.name = P_name
        print("Here comes " + self.name)
        
    def talk(self, P_message):        
        print(self.name + " says: '" + P_message + "'")        
        
    def walk(self):
        """ This let an instance of a man to walk """
        print(self.name + " walks")

# This class inherits from Man class
class superman(man):
    
    # Name of his secret identity
    secret_identity = ""
    
    def __init__(self, P_name, P_secret_identity):
        """ Class constructor that overrides its parent class constructor"""
        # Invokes the class constructor of the parent class #
        super(superman, self).__init__(P_name)
        # Now let's add a secret identity
        self.secret_identity = P_secret_identity
        print("...but his secret identity is '" + self.secret_identity + "' and he's a super-hero!")
        
    def fly(self):
        """ This let an instance of a superman to fly """
        print(self.secret_identity + " fly up in the sky")
        
    def x_ray(self):
        """ This let an instance of a superman to use his x-ray vision """
        print(self.secret_identity + " uses his x-ray vision")
        

# Declare some instances of man and superman
lois = man("Lois Lane")
jimmy = man("Jimmy Olsen")
clark = superman("Clark Kent", "Superman")

# Let's puth them into action!

print("\nLet's see what a man can do:\n")
jimmy.walk()
lois.talk("Oh no, we're in danger!")

print("\nLet's see what a superman can do:\n")
clark.walk()
clark.talk("This is a job for SUPERMAN!")
clark.fly()
clark.x_ray()

Initial URL

                                

Initial Description
This simple example will show you how to inherit a class from a parent class. I have to apologise for some grammar mistakes that I've probably put in the comments, but English is not my native language.

If you execute this code, the output will be:


Here comes Lois Lane

Here comes Jimmy Olsen

Here comes Clark Kent

...but his secret identity is 'Superman' and he's a super-hero!


--> Let's see what a man can do:


Jimmy Olsen walks

Lois Lane says: 'Oh no, we're in danger!'

--> Let's see what a superman can do:


Clark Kent walks

Clark Kent says: 'This is a job for SUPERMAN!'

Superman run at the speed of light

Superman fly up in the sky

Superman uses his x-ray vision

Initial Title
Example of class inheritance and method overriding in Python

Initial Tags
class

Initial Language
Python