Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python程序使用继承计算长方体的容量_Python - Fatal编程技术网

Python程序使用继承计算长方体的容量

Python程序使用继承计算长方体的容量,python,Python,我正在学习python,但我无法想出一个继承程序来计算一个大盒子可以包含的小盒子的数量。我有一部分代码是作为作业的一部分给出的,但不幸的是,没有给出解决方案。以下是给出的代码: class Box: def getVolume(self): vol= self.length*self.breadth* self.height return vol def __init__(self, length, breadth,height):

我正在学习python,但我无法想出一个继承程序来计算一个大盒子可以包含的小盒子的数量。我有一部分代码是作为作业的一部分给出的,但不幸的是,没有给出解决方案。以下是给出的代码:

class Box:
    def getVolume(self):
        vol= self.length*self.breadth* self.height
        return vol
    def __init__(self, length, breadth,height):
        self.length = length
        self.breadth = breadth
        self.height = height

class BigBox(Box):
    def __init__(self,length,breadth,height):
         Box.__init__(self,length,breadth,height)
         def getCapacity(self, sBox):
要求:大盒子可以装小盒子

Q:为函数getCapacityself、返回Capacity的BigBox中的sBox编写逻辑,该函数可以包含多少个小盒子

容量公式=大箱容量/小箱容量

使用以下内容测试代码:

smallBox = Box(1,1,1)
bigBox = BigBox (4,4,4)
capacity = bigBox.getCapacity(smallBox)
print("capacity:",capacity)
capacity: 64

有人能帮我吗?提前感谢。

因为您已经有了容量=大盒体积/小盒体积的公式,您可以简单地将BigBox.getCapacity方法定义为:

def getCapacity(self, sBox):
    return self.getVolume() // sBox.getVolume()
通过示例输入,这将输出以下内容:

smallBox = Box(1,1,1)
bigBox = BigBox (4,4,4)
capacity = bigBox.getCapacity(smallBox)
print("capacity:",capacity)
capacity: 64