Class in Object Oriented Programming
In object-oriented programming (OOP), a class is a blueprint for creating objects. It defines the properties and behaviors that an object can have, and provides a way to create and initialize objects of that type.
For example, you could create a class called "Dog" that has properties like "breed" and "age", and behaviors like "bark" and "fetch". You could then create individual dog objects that belong to this class, each with its own unique set of property values.
Here is an example of a simple class in Python:
class Dog:
def __init__(self, breed, age):
self.breed = breed
self.age = age
def bark(self):
print("Woof!")
def fetch(self, object):
print("Fetching the " + object)
dog1 = Dog("Labrador", 3)
dog2 = Dog("Poodle", 5)
print(dog1.breed) # Output: "Labrador"
print(dog2.age) # Output: 5
dog1.bark() # Output: "Woof!"
dog2.fetch("ball") # Output: "Fetching the ball"
In this example, the Dog class has two properties: breed and age, and two behaviors: bark and fetch. The __init__ method is a special method in Python classes that is called when an object is created, and is used to initialize the object's properties. The self parameter is a reference to the object itself, and is used to access the object's properties and methods from within the class.
0 Comments