I am learning poo and a doubt arose because this code does not work in the first way, but in the second way, which is when I put the "Car()" class inside the "car" variable, I want to know the Why does it work like this, because if I just call the class it doesn't work. I'm learning through videos so I don't have a teacher as such to whom to ask this question, I hope I don't cause problems here by asking.
*First Form
class Coche:
# atributos o propiedades (variables)
#caracteristicas del coche
color = "Rojo"
marca = "Ferrari"
Modelo = "Aventador"
velocidad = 300
caballaje = 500
plazas = 2
# Metodos ,son acciones que hace el objeto (coche) (antes se les llamaba funcion)
def setColor(self, paletaColor): # Ademas otra funcion de self,es que basicamente te va a dejar al usarla intereactuar con lo que haya dentro de la clase
self.color = paletaColor #con este metodo yo estoy cambiando el atributo o la variable de color
def getColor(self):
return self.color #con esta funcion,o motodo,yo estoy llamando al atributo
def setModelo(self, marca):
self.Modelo = marca
def getModelo(self):
return self.Modelo
def acelerar(self): #con self entre los parametros se puede acceder a todos los atributos de una clase
self.velocidad += 1
def frenar(self):
self.velocidad -=1
def getVelocidad(self):
return self.velocidad
def getMarca(self):
return self.marca
def setMarca(self, marcaElegida):
self.marca = marcaElegida
# Fin definicion clase
print("COCHE 1: ")
Coche().acelerar()
Coche().acelerar()
Coche().acelerar()
print(Coche().velocidad)
*Second Way here is where I put the Car class inside the car variable, and if it works when executed, I want to know why the other way doesn't work
class Coche:
# atributos o propiedades (variables)
#caracteristicas del coche
color = "Rojo"
marca = "Ferrari"
Modelo = "Aventador"
velocidad = 300
caballaje = 500
plazas = 2
# Metodos ,son acciones que hace el objeto (coche) (antes se les llamaba funcion)
def setColor(self, paletaColor): # Ademas otra funcion de self,es que basicamente te va a dejar al usarla intereactuar con lo que haya dentro de la clase
self.color = paletaColor #con este metodo yo estoy cambiando el atributo o la variable de color
def getColor(self):
return self.color #con esta funcion,o motodo,yo estoy llamando al atributo
def setModelo(self, marca):
self.Modelo = marca
def getModelo(self):
return self.Modelo
def acelerar(self): #con self entre los parametros se puede acceder a todos los atributos de una clase
self.velocidad += 1
def frenar(self):
self.velocidad -=1
def getVelocidad(self):
return self.velocidad
def getMarca(self):
return self.marca
def setMarca(self, marcaElegida):
self.marca = marcaElegida
coche = Coche()
print("COCHE 1: ")
coche.acelerar()
coche.acelerar()
coche.acelerar()
print(coche.velocidad)
what happens is that in the second form you are instantiating the class, in such a way that you can access its methods/attributes and that is why it works for you; that is the correct way.
In the first way, you are accessing the class directly, which is incorrect, since in OOP you cannot instantiate a class directly because it inherits from the general class or mother of all, which is
Object
.This is called instantiating an object: