HELLO WORLD--一起加油(🍺)!|

kingwzun

园龄:3年6个月粉丝:111关注:0

7-4 sdut-oop-5 计算长方体和四棱锥的表面积和体积(类的继承) python

Python __ 面向对象基础

import math as m
class Rect:
l = 0.0
h = 0.0
z = 0.0
def __init__(self, l, h, z):
self.l = l
self.h = h
self.z = z
def length(self):
return (self.l + self.h) * 2
def area(self):
return self.l * self.h
# 立方体类Cubic
class Cubic(Rect):
def __init__(self, l, h, z):
super().__init__(l, h, z)
def area(self):
return (self.h * self.l + self.l * self.z + self.h * self.z) * 2
def v(self):
return self.h * self.l * self.z
# 四棱锥类Pyramid
class Pyramid(Rect):
def __init__(self, l, h, z):
super().__init__(l, h, z)
def area(self):
return m.sqrt(self.h*self.h+4*self.z*self.z)*self.l/2+self.h*self.l+m.sqrt(self.l*self.l+4*self.z*self.z)*self.h/2
def v(self):
return self.h * self.l * self.z/3
while True:
try:
a, b, c = map(float, input().split())
C = Cubic(a, b, c)
P = Pyramid(a, b, c)
if a<=0 or b<=0 or c<=0:
print("0.00 0.00 0.00 0.00")
else:
# print(C.area, C.v, P.area, P.v)
print("{:.2f} {:.2f} {:.2f} {:.2f}".format(C.area(), C.v(), P.area(), P.v()))
except:
break

本文作者:kingwzun

本文链接:https://www.cnblogs.com/kingwz/p/16334162.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   kingwzun  阅读(514)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起