Introduction to Python Programming

 

Assignment 3

 

            Create a class and several subclasses to demonstrate class inheritance and polymorphism.

 

 

 

 

 

 

 


 

#!/usr/bin/env python

 

#-----------------------Geo class BEGINS------------------------------

 

class Geo:

    def __init__(self, name):  

        self.translate = (0,0,0)

        self.rotate = (0,0,0)

        self.scale = (1.0,1.0,1.0)

        self.pivot = (0,0,0)

        self.name = name

        self.geoType = "null"

        self.parent = "world"

        self.children = []

   

    def getType(self):

        return self.geoType

   

    def addChild(self, child):

        if (child.name in self.children):

            print '%s already a child of %s' % (child.name, self.name)

        elif (child.name == self.name):

            print '%s cannot be a child of itself' % self.name

        else:

            child.parent = self.name

            self.children.append(child)

            print '%s a child of %s' % (child.name, self.name)

   

    def setParent(self, parentName):

        if (parentName == self.name):

            print '%s cannot be parented to itself' % self.name

        else:

            self.parent = parentName

            print '%s parented to %s' % (parentName, self.name)

   

    def reName(self, newName):

        self.name = newName

       

    def setTranslate(self, tx, ty, tz):

        self.translate = (tx, ty, tz)

   

    def setRotate(self, rx, ry, rz):

        self.rotate = (rx, ry, rz)

   

    def setScale(self, sx, sy, sz):

        self.scale = (sx, sy, sz)

   

    def setPivot(self, px, py, pz):

        self.pivot = (px, py, pz)

   

#-----------------------Geo class ENDS------------------------------

 

#-----------------------Sphere class BEGINS------------------------------

class Sphere(Geo):

    def __init__(self, name):

        Geo.__init__(self, name)

        self.geoType = "sphere"

        self.radius = 1.0

        self.sections = 16

        self.spans = 8

       

    def setRadius(self, radius):

        self.radius = radius

       

    def setSections(self, sections):

        self.sections = sections

       

    def setSpans(self, spans):

        self.spans = spans

 

    def reName(self, newName):

        self.name = newName

        print "this is the Sphere class renamer"

 

#-----------------------Sphere class ENDS------------------------------

 

#-----------------------Cylinder class BEGINS------------------------------

 

class Cylinder(Sphere):

    def __init__(self, name):

        Sphere.__init__(self, name)

        self.geoType = "cylinder"

        self.height = 1.0

       

    def setHeight(self, height):

        self.height = height

 

    def reName(self, newName):

        self.name = newName

        print "this is the Cylinder class renamer"

 

#-----------------------Cylinder class ENDS------------------------------

 

#------------------------------------------------------------------------

 

myGeo = Geo("geo1")

myGeoName = myGeo.name

print myGeo.name

print myGeo.getType()

 

myGeo.setTranslate(-20, 10, 12)

print 'x = %d y = %d z = %d' % (myGeo.translate[0], myGeo.translate[1], myGeo.translate[2])

 

parentGeo = Geo("geo0")

parentGeo.setRotate(30, -45, 90)

 

firstChildGeo = Geo("geo2")

secondChildGeo = Geo("geo3")

 

myGeo.reName("tao_geo")

print myGeo.name

 

print 'default geo parent is the %s' % myGeo.parent

myGeo.setParent(parentGeo.name)

print "now %s has a real parent who is %s" % (myGeo.name, myGeo.parent)

 

myGeo.addChild(firstChildGeo)

myGeo.addChild(secondChildGeo)

 

print "%s's parent is %s" % (firstChildGeo.name, firstChildGeo.parent)

print "%s's parent is also %s" % (secondChildGeo.name, secondChildGeo.parent)

 

print "------------------------------------------------------"

 

mySphere = Sphere("sphere1")

print mySphere.name

 

print mySphere.geoType

print mySphere.getType()

 

mySphere.reName("skoshi")

print "my sphere's new name is %s" % mySphere.name

 

print "------------------------------------------------------"

 

myCylinder = Cylinder("cylinder1")

print "%s is this type of Geo class: %s" % (myCylinder.name, myCylinder.geoType)

 

myCylinder.setRadius(12.0)

print "myCylinder's new radius is %d" % myCylinder.radius

 

#-------------------------------------------------------------------