Introduction to the three characteristics of object-oriented programming

Introduction to the three characteristics of object-oriented programming

For the basics of object-oriented programming, please refer to https://blog.csdn.net/cnds123/article/details/128998309

Object-oriented three characteristics

Encapsulation

The operation of writing properties and methods into a class, or encapsulation is the encapsulation of data (properties) and operations (methods) in a class.

Through encapsulation, the internal implementation details can be hidden, and only the necessary interfaces are exposed for other objects to use. This improves code maintainability and reuse.

Inheritance

Inheritance means that one class (parent class/base class) can derive another class (subclass/derived class), and the subclass can inherit the properties and methods of the parent class.

Inheritance enables code to have a hierarchical structure, allowing common features and behaviors to be defined, modified and extended in subclasses. Inheritance can improve code reusability and extensibility.

Polymorphism

Polymorphism is a way of using objects. The subclass overrides the method of the parent class. When a method rewritten by the subclass is called using the reference of the parent class, what is actually executed is the implementation of the method in the subclass.

Polymorphism means that objects of the same type can behave differently in different situations.

Polymorphism is implemented through mechanisms such as function overloading, function rewriting, and abstract classes/interfaces, which brings higher flexibility and scalability to programming.

Special example explanation : When we use the reference of the parent class to call a method that is overridden by the subclass, what is actually executed is the implementation of the method in the subclass. Suppose we have a parent class Animal and two subclasses Cat and Dog, both inherit from the Animal class, and both override a method named makeSound(). If we use the reference or pointer of the parent class Animal to point to a subclass object and call the makeSound() method, what is actually executed is the specific implementation of the makeSound() method in the subclass.

★ Java example source code:

class Animal {
    public void makeSound() {
        System.out.println("Animal is making a sound");
    }
}

class Cat extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Cat is meowing");
    }
}

class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Dog is barking");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal animal1 = new Cat();
        Animal animal2 = new Dog();

        animal1.makeSound(); // 实际执行的是 Cat 类中的 makeSound() 方法
        animal2.makeSound(); // 实际执行的是 Dog 类中的 makeSound() 方法
    }
}

C++ sample source code:

#include <iostream>
using namespace std; 

class Animal {
public:
    virtual void makeSound() {
        cout << "Animal is making a sound" << endl;
    }
};

class Cat : public Animal {
public:
    void makeSound() override {
        cout << "Cat is meowing" << endl;
    }
};

class Dog : public Animal {
public:
    void makeSound() override {
        cout << "Dog is barking" << endl;
    }
};

int main() {
    Animal* animal1 = new Cat();
    Animal* animal2 = new Dog();

    animal1->makeSound(); // 实际执行的是 Cat 类中的 makeSound() 方法
    animal2->makeSound(); // 实际执行的是 Dog 类中的 makeSound() 方法

    delete animal1;
    delete animal2;

    return 0;
}

★ Python sample source code:

class Animal:
    def makeSound(self):
        print("Animal is making a sound")

class Cat(Animal):
    def makeSound(self):
        print("Cat is meowing")

class Dog(Animal):
    def makeSound(self):
        print("Dog is barking")

animal1 = Cat()
animal2 = Dog()

animal1.makeSound() # 实际执行的是 Cat 类中的 makeSound() 方法
animal2.makeSound() # 实际执行的是 Dog 类中的 makeSound() 方法

★ JavaScript sample source code:

<script>

class Animal {
    makeSound() {
        console.log("Animal is making a sound");
    }
}

class Cat extends Animal {
    makeSound() {
        console.log("Cat is meowing");
    }
}

class Dog extends Animal {
    makeSound() {
        console.log("Dog is barking");
    }
}

const animal1 = new Cat();
const animal2 = new Dog();

animal1.makeSound(); // 实际执行的是 Cat 类中的 makeSound() 方法
animal2.makeSound(); // 实际执行的是 Dog 类中的 makeSound() 方法

</script>

In the above code, multiple languages ​​are used to demonstrate the rewriting of parent class methods through subclasses. When this method is called on a specific object, the corresponding method implementation in the subclass is actually executed. Specifically, the references animal1 and animal2 of the Animal type point to objects of the Cat and Dog classes respectively. When we call the makeSound() method, what is actually executed is the makeSound() method in the subclass to which each object belongs.

Guess you like

Origin blog.csdn.net/cnds123/article/details/131931698