Definition and test function for class Cylinder. : subclass « Class « Python Tutorial

Home
Python Tutorial
1.Introduction
2.Data Type
3.Statement
4.Operator
5.String
6.Tuple
7.List
8.Dictionary
9.Collections
10.Function
11.Class
12.File
13.Buildin Function
14.Buildin Module
15.Database
16.Regular Expressions
17.Thread
18.Tkinker
19.wxPython
20.XML
21.Network
22.CGI Web
23.Windows
Python Tutorial » Class » subclass 
11.29.8.Definition and test function for class Cylinder.
import math

class Point:
   def __init__self, xValue = 0, yValue = ):
      self.x = xValue
      self.y = yValue

class CirclePoint ):
   def __init__self, x = 0, y = 0, radiusValue = 0.0 ):
      Point.__init__self, x, y )  # call base-class constructor
      self.radius = floatradiusValue )

   def areaself ):
      return math.pi * self.radius ** 2

   def __str__self ):
      return "Center = %s Radius = %f" Point.__str__self ), self.radius )


class CylinderCircle ):
   def __init__self, x, y, radius, height ):
      Circle.__init__self, x, y, radius )
      self.height = floatheight )

   def areaself ):
      return * Circle.areaself * math.pi * self.radius * self.height

   def volumeself ):
      return Circle.areaself * height

   def __str__self ):
      return "%s; Height = %f" Circle.__str__self ), self.height )


cylinder = Cylinder12232.55.7 )

print "X coordinate is:", cylinder.x
print "Y coordinate is:", cylinder.y
print "Radius is:", cylinder.radius
print "Height is:", cylinder.height

cylinder.height = 10
cylinder.radius = 4.25
cylinder.x, cylinder.y = 22
print "\nThe new points, radius and height of cylinder are:", cylinder

print "\nThe area of cylinder is: %.2f" % cylinder.area()

print "\ncylinder printed as a Point is:", Point.__str__cylinder )

print "\ncylinder printed as a Circle is:", Circle.__str__cylinder )
11.29.subclass
11.29.1.Creating Subclasses
11.29.2.Class Inheritance
11.29.3.__bases__ Class Attribute
11.29.4.Overriding Methods through Inheritance
11.29.5.__init__() is not invoked automatically when the subclass is instantiated.
11.29.6.Deriving Standard Types
11.29.7.Mutable Type Example
11.29.8.Definition and test function for class Cylinder.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.