使用 Class 和继承计算三角形的面积
Calculate the area of the Triangle using Class and Inheritance
大家好,我正在尝试使用 Heron 公式计算三角形的面积,即 area= sqrt(s(s-l1)(s-l2)(s-l3)) 。为此,我需要检查给定的边是否与我拥有的三角形相加。
但是,我在这里无法弄清楚如何在继承的 class 中使用它。
什么,我想做的是从父 class 获取输入并从继承的 class 计算面积。任何帮助表示赞赏。
使用的术语
1) l1, l2, l3 : 三角形的边
2) Checktri 方法用于检查给定的边加起来是否为三角形
3)Areatri是Triangledim的继承class,其中需要求出面积
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)
您可能需要以下代码:
import math
class Triangledim():
def __init__(self, l1, l2, l3):
self.l1 = l1
self.l2 = l2
self.l3 = l3
self.s = (self.l1+self.l2+self.l3) / 2.0
def checktri(self):
if (self.l1+self.l2>self.l3) and (self.l2+self.l3>self.l1) and (self.l1+self.l3>self.l2):
print("Perimeter of the triangle is: {}".format(self.s))
else:
print("not the right triangle proportions")
def findArea(self):
area = math.sqrt(self.s*(self.s-self.l1)*(self.s-self.l2)*(self.s-self.l3))
print("The area of the triangle is: {}".format(area))
if __name__ == "__main__":
p = Triangledim(7,5,10)
p.checktri()
p.findArea()
输出:
Perimeter of the triangle is: 11.0
The area of the triangle is: 16.24807680927192
如果您想使用遗产,以下内容将为您完成工作:
import math
class Triangledim():
def __init__(self, l1, l2, l3):
self.l1 = l1
self.l2 = l2
self.l3 = l3
self.s = (self.l1+self.l2+self.l3) / 2.0
def checktri(self):
if (self.l1+self.l2>self.l3) and (self.l2+self.l3>self.l1) and (self.l1+self.l3>self.l2):
print("Perimeter of the triangle is: {}".format(self.s))
else:
print("not the right triangle proportions")
class Areatri(Triangledim):
def findArea(self):
area = math.sqrt(self.s*(self.s-self.l1)*(self.s-self.l2)*(self.s-self.l3))
print("The area of the triangle is: {}".format(area))
if __name__ == "__main__":
p = Areatri(7,5,10)
p.checktri()
p.findArea()
输出:
Perimeter of the triangle is: 11.0
The area of the triangle is: 16.24807680927192
大家好,我正在尝试使用 Heron 公式计算三角形的面积,即 area= sqrt(s(s-l1)(s-l2)(s-l3)) 。为此,我需要检查给定的边是否与我拥有的三角形相加。
但是,我在这里无法弄清楚如何在继承的 class 中使用它。
什么,我想做的是从父 class 获取输入并从继承的 class 计算面积。任何帮助表示赞赏。
使用的术语 1) l1, l2, l3 : 三角形的边 2) Checktri 方法用于检查给定的边加起来是否为三角形 3)Areatri是Triangledim的继承class,其中需要求出面积
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)
您可能需要以下代码:
import math
class Triangledim():
def __init__(self, l1, l2, l3):
self.l1 = l1
self.l2 = l2
self.l3 = l3
self.s = (self.l1+self.l2+self.l3) / 2.0
def checktri(self):
if (self.l1+self.l2>self.l3) and (self.l2+self.l3>self.l1) and (self.l1+self.l3>self.l2):
print("Perimeter of the triangle is: {}".format(self.s))
else:
print("not the right triangle proportions")
def findArea(self):
area = math.sqrt(self.s*(self.s-self.l1)*(self.s-self.l2)*(self.s-self.l3))
print("The area of the triangle is: {}".format(area))
if __name__ == "__main__":
p = Triangledim(7,5,10)
p.checktri()
p.findArea()
输出:
Perimeter of the triangle is: 11.0
The area of the triangle is: 16.24807680927192
如果您想使用遗产,以下内容将为您完成工作:
import math
class Triangledim():
def __init__(self, l1, l2, l3):
self.l1 = l1
self.l2 = l2
self.l3 = l3
self.s = (self.l1+self.l2+self.l3) / 2.0
def checktri(self):
if (self.l1+self.l2>self.l3) and (self.l2+self.l3>self.l1) and (self.l1+self.l3>self.l2):
print("Perimeter of the triangle is: {}".format(self.s))
else:
print("not the right triangle proportions")
class Areatri(Triangledim):
def findArea(self):
area = math.sqrt(self.s*(self.s-self.l1)*(self.s-self.l2)*(self.s-self.l3))
print("The area of the triangle is: {}".format(area))
if __name__ == "__main__":
p = Areatri(7,5,10)
p.checktri()
p.findArea()
输出:
Perimeter of the triangle is: 11.0
The area of the triangle is: 16.24807680927192