En Python, la programación orientada a objetos (POO) es un paradigma de programación que utiliza objetos y clases en la programación. Su objetivo es implementar entidades del mundo real como herencia, polimorfismos, encapsulación, etc. en la programación. El concepto principal de los OOP es unir los datos y las funciones que trabajan en eso como una sola unidad para que ninguna otra parte del código pueda acceder a estos datos.
Conceptos principales de la programación orientada a objetos (POO)
- Clase
- Objetos
- Polimorfismo
- Encapsulación
- Herencia
- Abstracción de datos
Clase
Una clase es una colección de objetos. Una clase contiene los planos o el prototipo a partir del cual se crean los objetos. Es una entidad lógica que contiene algunos atributos y métodos.
Python
# Python3 program to # demonstrate defining # a class class Dog: pass
Python3
obj = Dog()
Python3
class Dog: # class attribute attr1 = "mammal" # Instance attribute def __init__(self, name): self.name = name # Driver code # Object instantiation Rodger = Dog("Rodger") Tommy = Dog("Tommy") # Accessing class attributes print("Rodger is a {}".format(Rodger.__class__.attr1)) print("Tommy is also a {}".format(Tommy.__class__.attr1)) # Accessing instance attributes print("My name is {}".format(Rodger.name)) print("My name is {}".format(Tommy.name))
Python3
class Dog: # class attribute attr1 = "mammal" # Instance attribute def __init__(self, name): self.name = name def speak(self): print("My name is {}".format(self.name)) # Driver code # Object instantiation Rodger = Dog("Rodger") Tommy = Dog("Tommy") # Accessing class methods Rodger.speak() Tommy.speak()
Python3
# Python code to demonstrate how parent constructors # are called. # parent class class Person(object): # __init__ is known as the constructor def __init__(self, name, idnumber): self.name = name self.idnumber = idnumber def display(self): print(self.name) print(self.idnumber) def details(self): print("My name is {}".format(self.name)) print("IdNumber: {}".format(self.idnumber)) # child class class Employee(Person): def __init__(self, name, idnumber, salary, post): self.salary = salary self.post = post # invoking the __init__ of the parent class Person.__init__(self, name, idnumber) def details(self): print("My name is {}".format(self.name)) print("IdNumber: {}".format(self.idnumber)) print("Post: {}".format(self.post)) # creation of an object variable or an instance a = Employee('Rahul', 886012, 200000, "Intern") # calling a function of the class Person using # its instance a.display() a.details()
Python3
class Bird: def intro(self): print("There are many types of birds.") def flight(self): print("Most of the birds can fly but some cannot.") class sparrow(Bird): def flight(self): print("Sparrows can fly.") class ostrich(Bird): def flight(self): print("Ostriches cannot fly.") obj_bird = Bird() obj_spr = sparrow() obj_ost = ostrich() obj_bird.intro() obj_bird.flight() obj_spr.intro() obj_spr.flight() obj_ost.intro() obj_ost.flight()
Python3
# Python program to # demonstrate private members # Creating a Base class class Base: def __init__(self): self.a = "GeeksforGeeks" self.__c = "GeeksforGeeks" # Creating a derived class class Derived(Base): def __init__(self): # Calling constructor of # Base class Base.__init__(self) print("Calling private member of base class: ") print(self.__c) # Driver code obj1 = Base() print(obj1.a) # Uncommenting print(obj1.c) will # raise an AttributeError # Uncommenting obj2 = Derived() will # also raise an AtrributeError as # private member of base class # is called inside derived class
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA