Saltar al contenido

cómo usar la herencia en el ejemplo de código de Python

Por fin después de mucho luchar hemos dado con la respuesta de esta incógnita que ciertos de nuestros usuarios de nuestra web han tenido. Si deseas compartir algo no dejes de dejar tu comentario.

Ejemplo 1: ¿que es una herencia secundaria en Python con el ejemplo?

# =============================================================================# Inhertance# =============================================================================classA:
    def feature1(self):print('Feature 1 in process...')
    def feature2(self):print('Feature 2 in process...')#Pt.1classB:
    def feature3(self):print('Feature 3 in process...')
    def feature4(self):print('Feature 4 in process...')
        
a1 =A() 

a1.feature1()
a1.feature2()

a2 =B()

a2.feature3()
a2.feature4()# THE ABOVE PROGRAM IS A PROGRAM WITHOUT USING INHERITANCE# WITH THE USE OF INHERITANCE IS BELOWclassA:
    def feature1(self):print('Feature 1 in process...')    
    def feature2(self):print('Feature 2 in process...')classB(A):deffeature3(self):print('Feature 3 in process...')# Pt.2
    def feature4(self):print('Feature 4 in process...')
        
a1 =A() 

a1.feature1()
a1.feature2()

a2 =B()

a2.feature3()
a2.feature4()# NOW TO CHECK OUT THE DIFFERENCE BETWEEN Pt.1# AND Pt.2 TRY RUNNIG THE CODE ON THE BASIS OF# INHERITANCE, IN OTHER WORDS TRY RUNNING ONLY # B CLASS IN Pt.2 AND THEN RUN ONLY a2# YOU WILL SEE A DIFFERENCE IN THE RUNNING OF # ONLY a2,,,, IT WILL STILL SHOW THAT FEATURE 3# AND 4 IS IN PROCESS,, THIS MEANS THAT B IS THE

Ejemplo 2: herencia en el ejemplo de Python 3

classRobot:
    
    def __init__(self, name):self.name = name
        
    def say_hi(self):print("Hi, I am "+self.name)classPhysicianRobot(Robot):pass

x =Robot("Marvin")
y =PhysicianRobot("James")print(x,type(x))print(y,type(y))

y.say_hi()

Ejemplo 3: herencia en Python

# =============================================================================# Inhertance# =============================================================================classA:
    def feature1(self):print('Feature 1 in process...')
    def feature2(self):print('Feature 2 in process...')#Pt.1classB:
    def feature3(self):print('Feature 3 in process...')
    def feature4(self):print('Feature 4 in process...')
        
a1 =A() 

a1.feature1()
a1.feature2()

a2 =B()

a2.feature3()
a2.feature4()# THE ABOVE PROGRAM IS A PROGRAM WITHOUT USING INHERITANCE# WITH THE USE OF INHERITANCE IS BELOWclassA:
    def feature1(self):print('Feature 1 in process...')    
    def feature2(self):print('Feature 2 in process...')classB(A):deffeature3(self):print('Feature 3 in process...')# Pt.2
    def feature4(self):print('Feature 4 in process...')
        
a1 =A() 

a1.feature1()
a1.feature2()

a2 =B()

a2.feature3()
a2.feature4()

Ejemplo 4: como heredar una clase en python

classBird():defeat(self):print("eating")classSparrow(Bird):defsound(self):print("ChiChi!")
 
birdobj =Sparrow()
birdobj.eat()
birdobj.sound()

Si tienes algún reparo o disposición de regenerar nuestro división te evocamos escribir una interpretación y con mucho gusto lo ojearemos.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *