How to reuse class values?

Asked

Viewed 61 times

0

I have three classes, the third one depends on the values of the other two (it depends on the E class Material and of A, Ix, Iu and Iz class Geometria. How can I do this? I know I need to correct the 3rd class, but I don’t know how (or even if what is there is right).

class Material(object):
    def __init__(self, nome, E, ni):
        self.nome = nome
        self.E = E
        self.ni = ni

class Geometria(object):
    def __init__(self, nome):
        self.nome = nome
        self.A = 0.
        self.Ix = 0.
        self.Iy = 0.
        self.Iz = 0.
        self.chi = 0.
    def retangular(self, b, h):
        self.A = b * h
        self.Iz = b * h ** 3 / 12.
        self.Iy = h * b **3 / 12
        self.Ix = ( 1 / 3 - 0.21 * b / h * ( 1 - b ** 4 / ( 12 * h ** 4 ) ) ) * h * b ** 3
        self.chi = 1.2

class SecTrans(Material, Geometria):
    def __init__(self, E, A, Iz, Iy, Ix):
        self.EA = E*A
        self.EIz = E*Iz
        self.EIy = E*Iy
        self.EIx = E*Ix
  • 1

    Can you describe what each class is? Because using multiple inheritance, you need to be very careful with the defined fields. For example, both Material how much Geometria own the field nome, then what should be the nome of SecTrans? Use multiple inheritance only when you are sure of what you are doing. If SecTrans only depends on the values of others, I don’t see why use inheritance in this case. Just passing them as a parameter would be enough.

  • The material class is in relation to the material used. For example, concrete, steel, wood, etc. Each has a different E and ni. The geometry class deals with the areas and moments of inertia of the cross section of the concrete bar, steel, wood studied and the Sectrans class calculates the stiffness of the bar in question, which depends on the material and geometry. I don’t know if I was too clear, I’m terrible at explaining

  • Then read about class composition.

1 answer

0

As commented by Anderson Carlos Woss, you need to be careful with multiple inheritance, in your case, I see it as follows: A cross section has one Material and a Geometria, but it is not by itself a material or a geometric shape/figure.

Can you understand the difference? It has both characteristics but is not a "descendant" of them.

An example of a class I would inherit Material could be Concreto for example, as Cubo could inherit from Geometria and so it goes.. (this of course, implying that classes would become abstract)

As for the code, something like this should do:

# Remove a herança
class SecTrans(object):
  pass  # Colocar o resto aqui

material = Material('Queijo', 3, 5)
geometria = Geometria('Circulo')
transversal = SecTrans(material.E, geometria.A, 6, 7, 8)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.