我正在写一个类,Tbeam(IPython笔记本2.2.0中的Python 2.7.8),它计算钢筋混凝土T梁的值。 Tbeam的凸缘和腹板被认为是Rectangle类的对象。我从类Tbeam中的类Rectangle实例化一个法兰和web,并创建方法来计算Tbeam的整体深度(d)和面积(面积)。
class Rectangle:
"""A class to create simple rectangle with dimensions width (b) and
height (d). """
def __init__(self, b, d ):
"""Return a rectangle object whose name is *name* and default
dimensions are width = 1, height = 1.
"""
self.width = b
self.height = d
def area(self):
"""Computes the area of a rectangle"""
return self.width * self.height
def inertia(self):
"""Computes the moment of inertia of a rectangle,
with respect to the centroid."""
return self.width*math.pow(self.height,3)/12.0
def perim(self):
"""Calculates the perimeter of a rectangle"""
return 2*(self.width+self.height)
def centroid(self):
"""Calculates the centroid of a rectangle"""
return self.height/2.0
def d(self):
"""Returns the height of the rectangle."""
return self.height
def bf(self):
"""Returns the width of the rectangle."""
return self.width
-
class Tbeam:
"""A class to create T beams with dimensions:
bf = width of top flange,
tf = thickness of top flange,
d = height of web,
bw = width of web. """
def __init__(self, bf,tf,d,bw):
self.bf = bf
self.tf = tf
self.d = d
self.bw = bw
self.flange = Rectangle(bf,tf)
self.web = Rectangle(bw,d)
def area(self):
area =self.flange.area + self.web.area
def d(self):
"""Returns the total height of the Tbeam"""
return self.flange.d + self.web.d
-
执行测试单元时
# Test Tbeam
t1 = Tbeam(60.0, 5.0,27.0,12.0)
print t1.d
print t1.area
-
我得到以下内容:
27.0
bound method Tbeam.area of <__main__.Tbeam instance at 0x7f8888478758
27.0是正确的,但我不明白打印t1.area的第二个响应。我认为我对区域的定义是不正确的,但我不知道如何纠正这个问题。
非常感谢
罗恩
答案 0 :(得分:1)
您正在打印t1.area
这是一种方法。您想打印调用方法的结果,因此print t1.area()
。
答案 1 :(得分:1)
面积方法定义为
def area(self):
area =self.flange.area + self.web.area
但应定义为
def area(self):
area =self.flange.area() + self.web.area()