This is the 18th day of my participation in the November Gwen Challenge. See the link for details: The Last Gwen Challenge 2021

Object – oriented three characteristics: encapsulation, inheritance, polymorphism

  1. Encapsulation: Improves program security
  • Wrap data (properties) and behavior (methods) in class objects, and call methods outside the object on properties inside the method. So you don’t have to worry about the internal implementation.
  • There are no special modifiers in Python for attributes to be private; if the attribute is not intended to be accessed, it is preceded by two underscores
  1. Inheritance: Improves code reuse
  2. Improve application scalability and maintainability.

1. The encapsulation

Let’s look at the use of private methods:

# Author: Internet Lao Xin
# Development time: 2021/4/4/0004 22:11
class Student:
    def __init__(self,name,age):
        self.name=name
        self.__age=age

    def show(self):
        print(self.name,self.__age)

laoxin=Student('Internet addict', 38) laoxin. The show ()print(laoxin.name)
print(laoxin.__age)

Copy the code

How can private properties be accessed? It can be accessed in the following format

Print (laoxin.Student_ _ age)

But in general, when we see private methods, we try not to access them.

2. The inheritance

Syntax: class Subclass name of the class (parent 1, parent 2) pass

  • If a class does not inherit from any classes, object is integrated by default
  • Python supports multiple inheritance
  • When you define a subclass, you must call the parent class’s constructor in its constructor
# Author: Internet Lao Xin
# Development time: 2021/4/4/0004 22:11
class Person(object):
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def info(self):
        print(self.name,self.age)

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

class Teacher(Person):
    def __init__(self,name,age,teachofyear):
        super(Teacher, self).__init__(name,age)
        self.teachofyear=teachofyear

stu=Student('Internet addict', 40100 (1) the teacher = the teacher'Higher than cold'Stu, 38, 10). The info () the teacher. The info ()Copy the code

Multiple inheritance is also supported in Python

For example, the principal can integrate the Person class and inherit the teacher class.

Implementation of polymorphism

Polymorphism means having multiple forms, which means that even if you do not know what type of object a variable refers to, you can still call a method through this variable, and dynamically decide which method in the object to call according to the type of object referenced by the variable during operation.

# Author: Internet Lao Xin
# Development time: 2021/4/4/0004 22:11
class Animal(object):
    def eat(self):
        print("Animals need to eat")
class Dog(Animal):
    def eat(self):
        print('Dogs eat bones')  # override method

class Cat(Animal):
    def eat(self):
        print('Cats eat fish')

class Person:  No classes are integrated
    def eat(self):
        print('People eat plants')

def fun(obj):
    obj.eat()

fun(Cat())
fun(Dog())
fun(Animal())

fun(Person()) But it can also call methods
Copy the code

The reason: Python is a dynamic language. Static languages must explicitly inherit to implement polymorphism before they can use it, while dynamic languages only care about having the method, not the class.