2014-10-15 2 views
-1

Я пишу класс, Tbeam (Python 2.7.8 в IPython notebook 2.2.0), который вычисляет значения для железобетонного T-луча. Фланец и полотно Tbeam считаются объектами класса Rectangle. Я создаю флажок и web из класса Rectangle в классе Tbeam и создаю методы для вычисления общей глубины (d) и области (области) Tbeam.python 2.7 Создание класса в другом классе

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. Я предполагаю, что мое определение для области неверно, но я не знаю, как исправить проблему.

Большое спасибо

Рон

ответ

1

Вы печати t1.area, который является способом. Вы хотите напечатать результат вызова метода, так что print t1.area().

+0

Я попытался это, но получить тот же результат. – Ron

+0

Спасибо за помощь в решении проблемы. – Ron

1

метод площадь определяется как

def area(self): 
    area =self.flange.area + self.web.area 

, но должен быть определен как

def area(self): 
    area =self.flange.area() + self.web.area() 
Смежные вопросы