Encapsulation, inheritance and polymorphism of python classes

1 encapsulation of python class

Simply put, it is to implement many properties and methods in the class, and access them externally through the interface.

For the hiding of attributes, let's look at an example:

class Person1:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def say(self):
        print("I name is {} and {} years old.".format(self.name, self.age))


if __name__ == '__main__':
    xiaoming = Person1("xiaoming", 12)
    xiaoming.say()

In this example, the Person1 class has two properties, we can directly access its properties through xiaoming and the symbol, for example:

    xiaoming.age = 13
    print(xiaoming.age)

In fact, the name and age of a person cannot be changed casually, so we need to use a technology to "hide", so that the outside world can only see it and cannot change it

code show as below:

class Person:
    def __init__(self, name, age):
        self._name = name
        self._age = age

    @property
    def name(self):
        return self._name

    @property
    def age(self):
        return self._age

    def say(self):
        print("I name is {} and {} years old.".format(self.name, self.age))

if __name__ == '__main__':
    xiaoming = Person("xiaoming", 12)
    xiaoming.say()

    xiaoming.age = 13
    print(xiaoming.age)

In this example, the actual variables are self._name, self._age, which cannot be seen by the outside world, and I think it is still self.name, self.age

The method is to cover the real variables through the decorator @property, which means hiding

2 classes of inheritance

Inheritance is achieved by adding the class to be inherited after the new class defined

Example:

from person2 import Person

class Student(Person):
    def __init__(self, name, age):
        super().__init__(name, age)


xiaozhang = Student("xiaozhang", 11)
xiaozhang.say()

There are two points to note here:

1 Person is the parent class, Student is the subclass, and the subclass has all the characteristics (properties and methods) of the parent class

2 The super() function can call all properties and methods of the parent class (except hidden ones)

Verification: print(super()._age) # An error will be reported here, saying that the corresponding variable cannot be found

3 classes of polymorphism

same method, different behavior

sample code

from person2 import Person

class Student(Person):
    def __init__(self, name, age):
        super().__init__(name, age)

    def say(self):
        print("My name is {} and {} years old and I am in 6 grade.".format(self.name, self.age))

class Programmer(Person):
    def __init__(self, name, age):
        super().__init__(name, age)

    def say(self):
        print("I am a Programmer.My name is {} and {} years old.".format(self.name, self.age))

xiaozhang = Student("xiaozhang", 11)
xiaozhang.say()

Join = Programmer("Join", 35)
Join.say()

Guess you like

Origin blog.csdn.net/luhouxiang/article/details/127035409