name handling y Herencia
This commit is contained in:
parent
d3dada2708
commit
83416c491d
24
clase_8/atributos.py
Normal file
24
clase_8/atributos.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
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__)
|
||||
|
||||
|
||||
17
clase_8/herencia.py
Normal file
17
clase_8/herencia.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
class Persona:
|
||||
|
||||
def __init__(self, nombre, edad):
|
||||
self.nombre = nombre
|
||||
self.edad= edad
|
||||
|
||||
def saludar(self):
|
||||
print(f"Hola, soy {self.nombre}")
|
||||
|
||||
class Alumno(Persona):
|
||||
|
||||
def estudiar(self):
|
||||
print("Estoy estudiando")
|
||||
|
||||
alumno1= Alumno("Juan", 17)
|
||||
print(alumno1.nombre)
|
||||
print(alumno1.edad)
|
||||
Loading…
Reference in New Issue
Block a user