Detailed explanation of polymorphism and encapsulation of Python learning

This article is mainly about polymorphism and encapsulation related content in python. Let's take a look. I hope it will be helpful for you to learn python .

  polymorphism

  one. definition

  Polymorphism: Objects obtained by instantiating different classes, calling different methods, and executing different logics.

  Inheritance of classes has two meanings: 1. Change, 2. Extension. Polymorphism is a specific implementation mechanism of the two-level meaning of a class, that is , calling the same method under objects instantiated by different classes, the implementation process is different.

  one. package

  The package can be understood as a multi-functional self-service beverage machine, and the machine is opaque and sealed, only different faucets are opened below. When customers need different beverages, they only need to turn on different faucet switches to get what they want. of beverages, but the customer does not know that a different beverage is produced inside the machine.

  In python , a wrapper can be a class or a function. Encapsulation is hiding data or properties inside from outside view.

  two. use of packaging

  There are two levels of encapsulation of a class. The first level of encapsulation : the class is a container, which is itself a kind of encapsulation; the second level of encapsulation: the private properties defined in the class are only used inside the class and cannot be accessed outside. .

  Python does not rely on language features to achieve the second level of encapsulation, but achieves the effect of encapsulation by complying with certain naming conventions for data attributes and function attributes.

  Convention 1. Any name starting with a double underscore should be a private property inside the class and cannot be accessed outside. As shown below:

  This is to call a class-private data property, what if you call a class-private function property? The result is shown below:

From the above example, we can find that all properties (including : data properties and function properties) named with double underscores in the class cannot be accessed normally outside.

  So is it true that the external can't access the internal data properties? We first print the attribute dictionary of the class. At this time, we will find that the leadteacher data attributes and activy that start with double underscores in the class have changed, as shown in the following figure:

So far we have found : at this time, the _ class name is added under the corresponding double-underscore attribute , so is there a way to access it externally? As shown below:

 Summary: Attributes named with single or double underscores are just a convention, not that python must not be able to access them. Actually, python doesn't really prevent you from accessing private properties, and modules do this convention too.

  So, after the subclass inherits the parent class, can the subclass access the private properties of the parent class? See below:

The third level of encapsulation : clearly distinguish the internal and external implementation logic, which cannot be known by the outside, and provide an access interface for the external use of the encapsulated internal logic (this is the real encapsulation). The meaning of encapsulation is to put the class The property inside is hidden, but what if the outside wants to use it? This is the third level of encapsulation. We can define a function called double-underlined property in the class category, so that we can indirectly access the hidden properties in the class by calling this function. To rewrite the above example, see the image below :

The code block for this section is :

  class School():

  price = 12000

  __leadteacher = "刘昌明"  #双下划线开头的数据属性

  def __init__(self,name,addr,type):

  self.name = name

  self.addr = addr

  self.type = type

  def showinfo(self):

  print("%s位于%s,是一所%大学,学费是%s"%(self.name,self.addr,self.type,self.price))

  def __activy(self):

  print("学校正在举办书法比赛")

  def returninfo(self):          #通过类中的函数来访问隐藏的属性,以供外部调用,这才是真正意义上的封装

  print("这个学校的现在是%s"%self.__leadteacher)

  self.__activy()

  # class Student(School):

  #    print(School.__leadteacher)

  s1 =School("长江大学","湖北省荆州市","国立一本")

  #直接在外部访问类的数据属性

  # print(s1.price)

  # print(s1.__leadteacher)  #报错

  # print(s1._leadteacher)  #报错

  #直接在外部访问类的函数属性

  # s1.__activy()    #报错

  #打印类的属性字典

  # print(School.__dict__)

  #再次访问类的内部属性

  # print(s1._School__leadteacher)

  # s1._School__activy()

  # student = Student()

  #通过类中的接口函数来访问类中隐藏的属性

  s1.returninfo()

 

 

来源:网络

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325382210&siteId=291194637