【发布时间】:2017-07-19 21:46:42
【问题描述】:
我对 python 2.7 和类中的 def 函数有疑问,因为绑定方法存在问题。这是学校的作业:D
代码如下:
从 abc 导入 ABCMeta,抽象方法 导入数学
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>>`
【问题讨论】:
-
对不起这是我第一次使用这个网站= =
-
间距太小,因为我不知道如何处理选项卡
-
不用担心,点击帖子下方的“编辑”并使用
{}按钮正确格式化代码。有一个预览会告诉你它是否有效 -
Bound method error的可能重复
-
只是缺少一些括号。 perimeter 和 Area 是函数,你要调用它们
标签: python