嘿,伙计们,我想用Heron公式找出三角形的面积,即面积=sqrt(s(s-l1)(s-l2)(s-l3))。对于这一点,我需要检查给定的边是否与我得到的三角形相加
但是,我不知道如何在继承的类中使用它
我想做的是从父类获取输入,并从继承的类计算面积。感谢您的帮助
使用的命名法 1) l1、l2、l3:三角形的边 2) Checktri方法用于检查给定边的总和是否为三角形 3) Areatri是继承的Triangledim类,其中需要查找区域
import math
class Triangledim:
def __init__(self, l1, l2, l3):
self.l1 = l1
self.l2 = l2
self.l3 = l3
#Check if the given measurements form a triangle
def checktri(self):
if (self.l1+self.l2>self.l3) & (self.l2+self.l3>self.l1) & (self.l1+self.l3>self.l2):
s = (self.l1 +self.l2+self.l3)/2
return ("Perimeter of the triangle is %f" %s)
else :
return("not the right triangle proportions")
class Areatri(Triangledim):
def __init__(self):
Triangledim.__init__(self)
area = math.sqrt(self.s(self.s-self.l1)(self.s-self.l2)(self.s-self.l3))
return area
p=Triangledim(7,5,10)
您可能需要以下代码:
输出:
如果您想使用heritage,以下内容将为您提供帮助:
输出:
相关问题 更多 >
编程相关推荐