25 lines
560 B
Python
25 lines
560 B
Python
class Persona:
|
|
|
|
def __init__(self, nombre, edad):
|
|
self.nombre = nombre
|
|
self.__edad = edad #oculto el verdadero nombre del atributo
|
|
|
|
def getEdad(self):
|
|
return self.__edad
|
|
|
|
def setEdad(self, edad):
|
|
if edad > 0:
|
|
self.__edad = edad
|
|
else:
|
|
print("Edad incorrecta")
|
|
|
|
persona1=Persona("Juan",30)
|
|
persona1.setEdad(10)
|
|
persona1.__edad=-500 #en realidad no estamos modificando el atributo original
|
|
persona1.__edad=-100
|
|
print(persona1.getEdad())
|
|
print(persona1.__edad)
|
|
print(persona1.__dict__)
|
|
|
|
|