Quotes help make search much faster. Example: "Practice Makes Perfect"

Sunday, December 2, 2012

Codecademy: Inheritance Syntax


#sample solution


class Shape(object):
"""Makes shapes!"""
def __init__(self, number_of_sides):
self.number_of_sides = number_of_sides

def printNumberOfSides(self):
print "This shape has %s sides." % (self.number_of_sides)

# Add your Triangle class below!
class Triangle(Shape):
number_of_sides = 3
def __init__(self, side1, side2, side3):
self.side1 = side1
self.side2 = side2
self.side3 = side3

#test
my_triangle = Triangle(1,2,3)
#notice how we can call the method printNumberOfSides() when
#class Triangle has no such method explicitly written
my_triangle.printNumberOfSides()

No comments:

Post a Comment


This is an example of scrolling text using Javascript.

Popular Posts