python绑定方法错误

时间:2017-02-28 19:13:00

标签: python

我有一个关于python 2.7和类中的def函数的问题,因为有关于绑定方法的问题。这是学校的任务:D

以下是代码:

来自abc import ABCMeta,abstractmethod 导入数学

class Shapes(object):
    __metaclass__= ABCMeta
    @abstractmethod

    def __init__(self):
        pass

class TwoDShapes(Shapes):
    def __init__(self):
        pass

class ThreeDShapes(Shapes):
def __init__(self):
        pass
================= Main.py =====================
from BasicClassShapes import*

class Rectangle(TwoDShapes):
    def __init__(self, nameOfShape, length, width, numberofSides, numberOfVertices):
        self.nameOfShape = "Rectangle"
        self.length = length
        self.width = width
        self.numberofSides = 4
        self.numberOfVertices = 4
        super(Rectangle,self).__init__()

    def perimeter(self):
        self.perimeter = length*2 + width*2
    def Area(self):
        self.Area = length*width

====================================

def PrintALL():

    A = Rectangle("Rectangle", "10", "20", "4",  "4")

    print "This is the name of the shape: ", A.nameOfShape
    print "This is the length: ", A.length
    print "This is the width: ", A.width
    print "This is the number of side: ", A.numberofSides
    print "This is the number of vertice: ", A.numberOfVertices
    print "This is the perimeter: ", A.perimeter
    print "This is the area: ", A.Area
    print "\n"
PrintALL()


=============== Result =======================
This is the name of the shape:  Rectangle
This is the length:  10
This is the width:  20
This is the number of side:  4
This is the number of vertice:  4
This is the perimeter:  <bound method Rectangle.perimeter of <__main__.Rectangle object at 0x03BEFC90>>

This is the area:  `<bound method Rectangle.Area of <__main__.Rectangle object at 0x03BEFC90>>`

4 个答案:

答案 0 :(得分:0)

您可以在周边函数中使用返回值:

def perimeter(self):
    return self.length*2 + self.width*2

然后拨打A.perimeter()而不是A.perimeter

print "This is the perimeter: ", A.perimeter()

同样适用于Area。

def Area(self):
    return self.length*self.width


print "This is the area: ", A.Area()

编辑:抱歉,我赶紧回答,并没有费心去检查。以下是Rectangle类和PrintALL()函数的工作替代品。我也在上面编辑过。

最好将数字类型(不是字符串)传递给函数,并且可以通过使用浮点数而不是整数来避免舍入错误。

class Rectangle(TwoDShapes):
    def __init__(self, nameOfShape, length, width, numberofSides, numberOfVertices):
        self.nameOfShape = "Rectangle"
        self.length = length
        self.width = width
        self.numberofSides = 4
        self.numberOfVertices = 4
        super(Rectangle,self).__init__()

    def perimeter(self):
        return self.length*2.0 + self.width*2.0
    def Area(self):
        return self.length*self.width

def PrintALL():

    A = Rectangle("Rectangle", 10.0, 20.0, 4.0, 4.0)

    print "This is the name of the shape: ", A.nameOfShape
    print "This is the length: ", A.length
    print "This is the width: ", A.width
    print "This is the number of side: ", A.numberofSides
    print "This is the number of vertice: ", A.numberOfVertices
    print "This is the perimeter: ", A.perimeter()
    print "This is the area: ", A.Area()
    print "\n"
PrintALL() 

输出:

This is the name of the shape:  Rectangle
This is the length:  10.0
This is the width:  20.0
This is the number of side:  4
This is the number of vertice:  4
This is the perimeter:  60.0
This is the area:  200.0

答案 1 :(得分:0)

如果形状不会改变,则它们不需要是函数:

class Rectangle(TwoDShapes):
    def __init__(self, nameOfShape, length, width, numberofSides, numberOfVertices):
        super(Rectangle,self).__init__()
        self.nameOfShape = "Rectangle"
        self.length = length
        self.width = width
        self.numberofSides = 4
        self.numberOfVertices = 4
        self.perimeter = length*2 + width*2
        self.Area = length*width

将使用您编写的代码。您不需要函数,因为您可以在perimeterArea初始化时对其进行数学运算。

答案 2 :(得分:0)

from BasicClassShapes import*

###############################################

class Rectangle(TwoDShapes):
    def __init__(self, nameOfShape, length, width, numberofSides, numberOfVertices):
        self.nameOfShape = "Rectangle"
        self.length = int(length)
        self.width = int(width)
        self.numberofSides = 4
        self.numberOfVertices = 4
        super(Rectangle,self).__init__()

    def perimeter(self):
        return self.length*2 + self.width*2
    def Area(self):
        return  self.length*self.width

###########################################

def PrintALL():

    A = Rectangle("Rectangle", "10", "20", "4",  "4")

### Specs of Rectangle ###
    print "This is the name of the shape: ", A.nameOfShape
    print "This is the length: ", A.length
    print "This is the width: ", A.width
    print "This is the number of side: ", A.numberofSides
    print "This is the number of vertice: ", A.numberOfVertices
    print "This is the perimeter: ", A.perimeter()
    print "This is the area: ", A.Area()
    print "\n"
PrintALL()

答案 3 :(得分:0)

class New:
    def __init__(self,a,b):
        self.a = a
        self.b = b


    def addition(self):
        return (self.a + self.b)

p = New(3,4)

print(p.a)
print(p.b)

print(p.addition())

看下面的代码片段,您需要使用()调用函数,将函数作为p.addition()并将变量作为p.a