Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.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_Class_Inheritance_Area - Fatal编程技术网

Python 使用类和继承计算三角形的面积

Python 使用类和继承计算三角形的面积,python,class,inheritance,area,Python,Class,Inheritance,Area,嘿,伙计们,我想用Heron的公式,即面积=sqrt(s(s-l1)(s-l2)(s-l3))找出三角形的面积。对于这一点,我需要检查给定的边是否与我得到的三角形相加 但是,我不知道如何在继承的类中使用它 我想做的是从父类获取输入,并从继承的类计算面积。感谢您的帮助 使用的命名法 1) l1、l2、l3:三角形的边 2) Checktri方法用于检查给定边的总和是否为三角形 3) Areatri是继承的Triangledim类,其中需要查找区域 import math class Triangl

嘿,伙计们,我想用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)

您可能需要以下代码:

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
Perimeter of the triangle is: 11.0
The area of the triangle is: 16.24807680927192
如果您想使用heritage,以下内容将为您提供帮助:

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
Perimeter of the triangle is: 11.0
The area of the triangle is: 16.24807680927192

这是真的,我也可以这样做,但想象一下,你需要一个遗产,如何解决这个问题?我需要了解的是,如何在heritage中使用self.s?我编辑了我的答案,并使用heritage包含了相同的结果。明白了,我现在明白了。因此,对于以s值派生的子类,s值需要定义为self.s?否则子类将无法使用它?我说的对吗?好的,所以不管你在方法中为例如checktri定义了什么(我在那里得到了值s,不会被继承到子类),你都可以用
self.variable
定义构造函数
\uuuu init\uuuuu
中的所有内容,然后对继承的类执行它。这不是你使用继承的方式。我同意,我是新手,我还在想办法。因此,如果我需要继承父类的方法,我需要使用一个指针,在本例中是“self”,并在那里声明我的属性,否则我不能将它用于我的子类?我说得对吗?不,我的意思是没有理由让一个类
Areatri
Triangledim
继承
Triangledim
表示三角形(即使用其边的长度)。计算一个三角形的面积是你对一个三角形所做的事情,而不是一种三角形(这是继承应该建模的)。明白了。谢谢,我会记住的。