试图理解Python类和自己的简单案例

时间:2017-03-27 22:29:44

标签: python class self

我对Python很陌生,但学习。我今天和教授交谈之后,我试图理清课堂和自我。这是一个简单的实验,我正在尝试使用一个"调用",主程序,以及一个带有被调用函数的单独类,从Main调用并从类内部调用。我已经写了足够多的文章来尝试测试一些灵活性,但我现在正在调试,我担心我会把A BUNCH的愚蠢垃圾放进去试图让它运行起来。有人可以看到需要做什么,让它运行没有任何多余的复杂功能?谢谢!

#######################################################################################################
#
# BoxDemoMain.py calling program
# demonstration of using and calling BoxClass.py pgm
#
#
import numpy as np
import BoxClassPgm
#
#
def ReportBoxDimensions(anySizeBox):
    Ht = anySizeBox.boxHeight
    Wd = anySizeBox.boxWidth
    Dp = anySizeBox.boxDepth
    print "The Height, Width, and Depth of this box is " + str(Ht) + "," + str(Wd) + "," + str(Dp)
    print "The Surface Area of this box is " + str(BoxClassPgm.BoxClass(anySizeBox).CalcSurfArea())
    print "The Volume of this box is " + str(BoxClassPgm.BoxClass(anySizeBox).CalcVolume())
#
#
largeBox = BoxClass(6,8,4)
mediumBox = BoxClass(4,5,3)
smallBox = BoxClass(3,3,3)
#
#
ReportBoxDimensions(largeBox)
ReportBoxDimensions(mediumBox)
ReportBoxDimensions(smallBox)
#######################################################################################################
#
# BoxClassPgm.py class program
# class demonstration
#
#
import numpy as np
#
#
class BoxClass:

    def __init__(self,boxHeight=0,boxWidth=0,boxDepth=0):
        self.boxHeight = boxHeight
        self.boxWidth = boxWidth
        self.boxDepth = boxDepth

    def CalcVolume(self):
        print "height is " + str(self.boxHeight)
        boxVol = self.boxHeight * self.boxWidth * self.boxDepth
        return boxVol

    def CalcSurfArea(self):
        areaOf3Faces = BoxClass().CalcFaceAreas()
        boxSurfArea = 2 * areaOf3Faces
        return boxSurfArea

    def CalcFaceAreas(self):
        frontBack = self.boxHeight * self.boxWidth
        leftRight = self.boxHeight * self.boxDepth
        topBottom = self.boxWidth * self.boxDepth
        box3Faces = frontBack + leftRight + topBottom

0 个答案:

没有答案