2017-02-28 70 views
0

我有一個關於python 2.7和def函數的問題,因爲有關於綁定方法的問題。這是從學校分配:dpython綁定方法錯誤

下面的代碼:

從ABC進口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>>` 
+0

抱歉,這是我第一次使用本網站= = – LDs

+0

可憐的間距怎麼我不知道做什麼用的標籤的事情做 – LDs

+0

不用擔心,點擊下面您的文章「編輯」和適當的格式化代碼使用'{}'按鈕。有預覽會告訴你它是否有效 – chrki

回答

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() 

OUTPUT:

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 
+0

NameError:沒有定義全局名'length' – LDs

+0

@LDs'self.length'等 – TemporalWolf

+0

def perimeter(self): return self.length * 2 + self。寬度* 2 def Area(self): return self.length * self.width print「This is the the perimeter:」,A.perimeter() print「This is the area:」,A.Area()' TypeError:無法乘以類型'str''的非int序列 – LDs

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執行數學運算。

+0

感謝您的幫助,但我們的老師說我們必須包括功能(體積,面積,表面積,周長,打印全部),這是紙上輸入的要求。 – LDs

+0

我想他們想要的功能,因爲如果長度或寬度修改周長和麪積值可以重新調用他們的函數重新計算。 – feedMe

+0

@feedMe真。如果你期望能夠修改形狀,他們應該是功能。 – TemporalWolf

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()