Makindo Medical Notes.com |
|
|---|---|
| Download all this content in the Apps now Android App and Apple iPhone/Pad App | |
| MEDICAL DISCLAIMER:The contents are under continuing development and improvements and despite all efforts may contain errors of omission or fact. This is not to be used for the assessment, diagnosis or management of patients. It should not be regarded as medical advice by healthcare workers or laypeople. It is for educational purposes only. Please adhere to your local protocols. Use the BNF for drug information. If you are unwell please seek urgent healthcare advice. If you do not accept this then please do not use the website. Makindo Ltd | |
Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" to design software. Objects are instances of classes, which are blueprints that define the properties (attributes) and behaviors (methods) of the objects. OOP helps to organize code in a modular, reusable, and maintainable way.
class Dog:
def __init__(self, name, breed):
self.name = name
self.breed = breed
def bark(self):
return f"{self.name} is barking."
my_dog = Dog("Buddy", "Golden Retriever")
print(my_dog.bark()) # Output: Buddy is barking.
class Person:
def __init__(self, name, age):
self.__name = name # Private attribute
self.__age = age # Private attribute
def get_name(self):
return self.__name
def set_age(self, age):
if age > 0:
self.__age = age
person = Person("Alice", 30)
print(person.get_name()) # Output: Alice
person.set_age(31)
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
pass
class Dog(Animal):
def speak(self):
return f"{self.name} says Woof!"
class Cat(Animal):
def speak(self):
return f"{self.name} says Meow!"
dog = Dog("Buddy")
cat = Cat("Whiskers")
print(dog.speak()) # Output: Buddy says Woof!
print(cat.speak()) # Output: Whiskers says Meow!
class Bird: def speak(self): return "Chirp!" class Dog: def speak(self): return "Woof!" def make_animal_speak(animal): print(animal.speak()) bird = Bird() dog = Dog() make_animal_speak(bird) # Output: Chirp! make_animal_speak(dog) # Output: Woof!
from abc import ABC, abstractmethod class Vehicle(ABC): @abstractmethod def start_engine(self): pass class Car(Vehicle): def start_engine(self): return "Car engine started." car = Car() print(car.start_engine()) # Output: Car engine started.
Object-Oriented Programming (OOP) is a programming paradigm centered around objects and classes. It encompasses key concepts such as encapsulation, inheritance, polymorphism, and abstraction. Adhering to OOP principles such as DRY, KISS, and the SOLID principles promotes modular, reusable, scalable, maintainable, and extensible code. OOP is widely used in many modern programming languages, including Python, Java, C++, and C#.