python - 'class' 任务需要指导(初学者)

标签 python python-2.7 class

它看起来令人生畏,但请耐心等待,它并不像看起来那么难。我这里有一个关于光束偏转的代码。此时只是一些数学和数字。只有最后一部分需要注意。

class beam(object):
"""This class is models the deflection of a simply supported beam under
multiple point loads, following Euler-Bernoulli theory and the principle of
superposition.
"""

    def __init__(self, E, I, L):
        """The class costructor.
        """
        self.E = 8.0E9  # Young's modulus of the beam in N/m^2
        self.I = 1.333E-4  # Second moment of area of the beam in m^4
        self.L = 5.0  # Length of the beam in m
        self.Loads = [(0.0, 0.0)]  # the list of loads applied to the beam
        self.name = "beam"

    def setLoads(self, Loads):
        '''This function allows multiple point loads to be applied to the beam
        using a list of tuples of the form (load, position)
        '''
        self.Loads = Loads

给出了“def __ init __”和“def setLoads”,所以上面的不需要改变。我输入了 self.E、I 和 L 的值,因为我认为我在那里需要它们,但这些数字可以替换回它们之前的字母。

    def beamDeflection(self, Load, x):
        """Just a simple calculation, really.
        """
        E = 8.09 * (10 ** 9)
        I = 1.333 * (10 ** -4)
        L = 5
        a = 2.5
        b = a + (x - a)
        (P1, a) = Load
        if 0 <= x <= 2.5:
            beamDeflection = ((P1*b*x)/(6*L*E*I))*((L**2)-(x**2)-(b**2))
        else:
            if 2.5 < x <= 5:
                beamDeflection = ((P1*b)/(6*L*E*I)) / (((L/b)*((x-a)**3)) -
                                                       (x**3) + (x*((L**2) -
                                                                 (b**2))))
        return beamDeflection

上面的“beamDeflection”是我输入的简单代码,它只是使用已经给出的公式计算光束的偏转。本质上,如果在梁的左侧放置一个重物,它会计算出一个数字,另一侧也会计算出相同的数字。

    def getTotalDeflection(self, x):
        """The function getTotalDeflection(self, x) should go through each load tuple in the
        list.Loads and calculate the beam deflection at point x (Hint: the function you just 
        created could be handy here!). getTotalDeflection should return the total deflection at x, 
        which is the sum over each of the individual deflections.
        """

我的理解是我需要一个“for”循环来遍历每个加载元组,同时涉及 self.load。我不确定如何将这两件事结合在一起。如果有人可以帮助我,我将非常非常感激。

最佳答案

你要找的大概是这个(否则请说明):

def getTotalDeflection(self, x):
    return sum(self.beamDeflection(loadval, x) for loadval in self.Loads)

关于python - 'class' 任务需要指导(初学者),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34276954/

相关文章:

python - 在多对多关系中查询

python - 什么时候使用 `<>` 和 `!=` 运算符?

linux - 无法使用 IMU 制造商的 python2.7 示例脚本传递 OSC 数据

java - java类文件加载/链​​接/初始化阶段的查询

python 3, window 7 :ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

python - 证书错误 : hostname doesn't match

java - Apache Thrift 中的对称加密 (AES)

python - 是否有避免长行代码的 Python 约定?

swift - 关联枚举案例将类名

python - 为什么Python的指向随着这个函数的微小改变而改变?