试图理解Python类和

2024-10-01 00:28:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python还很陌生,但是我在学习。今天和教授谈过之后,我正在整理课堂和自我。下面是一个简单的实验,我尝试用一个“调用”的主程序和一个单独的类来调用函数,从Main调用,从类内部调用。我已经写了足够多的东西来测试一些灵活性,但是我现在正在调试,我担心我会在这里面放一堆愚蠢的垃圾来让它运行。有人能看看需要做什么吗,请让它运行没有任何额外的并发症?谢谢!你知道吗

#######################################################################################################
#
# 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

Tags: ofpyimportselfisdefprintstr