Three characteristics of object-oriented : encapsulation , Inherit , polymorphic

List of articles

    • 1. encapsulation
    • 2. Inherit
    • The realization of polymorphism

  1. encapsulation : Improve the security of the program
  • Put the data ( attribute ) And behavior ( Method ) Wrapping in class objects , Inside the method, the property is called outside the object . So there's no need to care about the internal implementation .
  • stay python There is no special modifier for private properties in , If the property does not want to be accessed , Use two underscores in front
  1. Inherit : Improve code reusability
  2. Improve the scalability and scalability of the program Maintenance .

1. encapsulation

Let's look at the use of private methods :

#  author : Internet veteran Xin #  Development time :2021/4/4/0004 22:11class Student:
    def __init__(self,name,age):
    def show(self):
        print(,self.__age)laoxin=Student(' Internet veteran Xin ',38)

 Insert picture description here
How can private properties be accessed ?
You can access it in the following form

 Insert picture description here

print(laoxin.Student_ _ age) In the form of

But in general , We see private methods , Try not to visit .

2. Inherit

Grammar format :
class Subclass class name ( Parent class 1, Parent class 2)

  • If a class does not inherit any classes , Default Integration object
  • python Support for multiple inheritance
  • When defining subclasses , The constructor of the parent class must be called in its constructor
#  author : Internet veteran Xin #  Development time :2021/4/4/0004 22:11class Person(object):
    def __init__(self,name,age):
    def info(self):
        print(,self.age)class Student(Person):
    def __init__(self,name,age,sut_no):
class Teacher(Person):
    def __init__(self,name,age,teachofyear):
        super(Teacher, self).__init__(name,age)self.teachofyear=teachofyear
stu=Student(' Internet veteran Xin ',40,1001)teacher=Teacher(' Gao Shenghan ',38,10)

stay Python Multiple inheritance can also be supported in

Like the headmaster , It can integrate person class , And can inherit teacher class .

The realization of polymorphism

Polymorphism means having multiple forms , intend , Even if you don't know what type of object a variable refers to , You can still call methods through this variable , In the process of running, according to the object type referenced by the variable , Dynamically determine which object to call the method in .

#  author : Internet veteran Xin #  Development time :2021/4/4/0004 22:11class Animal(object):
    def eat(self):
        print(" Animals need to eat ")class Dog(Animal):
    def eat(self):
        print(' Dogs eat bones ')  # Rewriting methods 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): # But he can also call methods 

reason :
Python Is a dynamic language , Static language to achieve polymorphism , Inheritance must be made clear , And then you can use it , Dynamic languages are only concerned with having this method , About classes, no matter .