"Python Programming" 012 – Python object-oriented programming: basic knowledge of classes, objects, properties, methods, inheritance and polymorphism

Python is a powerful programming language that supports the Object-Oriented Programming (OOP) paradigm. Object-oriented programming allows you to organize your code into reusable and modular parts. In Python, you can use classes, objects, and methods to implement the concepts of object-oriented programming.

1. Classes

In Python, a class is a blueprint for creating objects. It defines the set of properties and methods that the object will have. To define a class in Python, you use the class keyword, followed by the name of the class and a colon. For example:

class Car:
    pass

The above example creates a class called "Car" that has no properties or methods. Through this class, you can create objects (instances).

2. Objects

Objects are instances of classes. Creating objects in Python is as simple as calling a class, just like a function. For example, if there is a class called "Car", you can create an object of that class like this:

my_car = Car()

This creates an object of class "Car" and assigns it to the variable my_car.

3. Attributes

Properties are data associated with an object. In a Python class, you define properties by creating variables inside the class. You can access the properties of an object using dot notation. For example:

class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year

my_car = Car('Toyota', 'Camry', 2022)
print(my_car.make)

In the example above, the "Car" class has three properties: make, model and year. We also define a constructor method __init__ that sets these properties when creating the object. Finally, we create an object of class "Car", assign it to the variable my_car, and then print out its make properties.

4. Methods

Methods are functions associated with an object. In a Python class, you define methods by creating functions inside the class. You can call methods of an object using dot notation. For example:

class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year

    def start(self):
        print(f"The {self.make} {self.model} is starting.")

my_car = Car('Toyota', 'Camry', 2022)
my_car.start()

In the above example, the "Car" class has a method named start that prints a message when called. We also created an object of class "Car", assigned it to the variable my_car, and then called its start method.

5. Inheritance

Inheritance is a mechanism in object-oriented programming that allows you to create a new class based on an existing class and inherit its properties and methods. The new class is called a subclass and the existing class is called a superclass. In Python, you can define a subclass by using the class keyword, followed by the name of the subclass and the name of the parent class (enclosed in parentheses). For example:

class ElectricCar(Car):
    def __init__(self, make, model, year, battery_size):
        super().__init__(make, model, year)
        self.battery_size = battery_size

    def charge(self):
        print(f"The {self.make} {self.model} is charging.")

my_electric_car = ElectricCar('Tesla', 'Model S', 2022, 100)
my_electric_car.charge()

In the above example, we have defined a subclass called "ElectricCar" which is a subclass of the "Car" class. The "ElectricCar" class has an additional property battery_size and a new method charge. We also created an object of the "ElectricCar" class, assigned it to the variable my_electric_car, and then called its charge method.

6. Polymorphism

Polymorphism is an important concept in object-oriented programming. It allows you to use a parent class or interface type to refer to an object of its subclass, and execute the corresponding method at runtime based on the actual object type. This allows you to write generic code that can handle different types of objects as long as they share the same interface or parent class.

In Python, the implementation of polymorphism relies on the features of dynamic type checking and dynamic binding. Dynamic type checking means determining the type of an object at runtime, not at compile time. Dynamic binding allows the appropriate method to be called at runtime based on the type of object.

Let us illustrate the concept of polymorphism through an example. Suppose we have a parent class Animal, and two subclasses Dog and Cat, both of which inherit from the parent class  Animal. These subclasses implement a method named make_sound .

class Animal:
    def make_sound(self):
        pass

class Dog(Animal):
    def make_sound(self):
        print("Woof!")

class Cat(Animal):
    def make_sound(self):
        print("Meow!")

Now we can create instances of Dog and Cat and call theirs using the reference of the parent class Animal make_sound method.

dog = Dog()
cat = Cat()

animal_list = [dog, cat]

for animal in animal_list:
    animal.make_sound()

In the example above, we created a list  containing instances of Dog and Cat . We then use  to loop through each element in the list and call their  method. Due to the nature of dynamic binding, the actual type of each object is determined and the corresponding  method is called. animal_listformake_soundmake_sound

The output will be:

Woof!
Meow!

As you can see, whether it is Dog or Cat, they all go through the parent class Animal Reference to call the method. This is the embodiment of polymorphism. The same method name make_sound is called by objects of different types, but the specific methods executed are different depending on the actual type of the object.

Polymorphism allows us to write more flexible and extensible code. By defining a common interface or parent class, we can write code that applies to multiple subclasses. This flexibility allows us to design and organize our programs in a more modular and reusable way.

Guess you like

Origin blog.csdn.net/zclmoon/article/details/132175675