Polymorphic
A Defined
Polymorphism: There are different classes that instantiate the resulting object, call different methods, and execute the logic differently.
The inheritance of a class has two meanings: 1. Change, 2. Expansion. Polymorphism is a concrete implementation mechanism of the two-level meaning of a class, that is to call the same method under the object of different class instantiation, the process of implementation is not the same.
A Packaging
Packaging can be understood as a multifunctional self-service beverage machine, and the machine is opaque sealed, only in the lower open a different faucet, when the customer needs a different drink, just to open a different tap switch to get the drink you want, but customers do not know that the inside of the machine is to produce different drinks.
In Python, encapsulation can be a class, which can be a function. Encapsulation is to hide the data or attributes inside, not to be seen externally.
Two Use of encapsulation
Class encapsulation two layers, the first layer of encapsulation: a class is a container, which is itself a package, the second layer of encapsulation: The class defines a private property, only used inside the class, external unreachable.
Python does not rely on language features to achieve the second level of encapsulation, but by adhering to certain data properties and function attributes of the naming convention to achieve the effect of encapsulation.
Convention 1. Any name that begins with a double underscore should be a private property within the class and cannot be accessed externally. Such as:
This is the data property of the calling class private, if the class private function property is called? Results See:
From the above example, we can see that all the attributes (including: Data properties and Function properties) named in the class with double underscores are not accessible externally.
Is it true that external data properties cannot be accessed? We'll start by printing the class's property dictionary, and we'll find a double underscore in the class.
Leadteacher Data Properties andThe Activy has changed, as shown in:
At this point, we found that: at this time in the corresponding double underline property under the _ Class name, then there is a way to access the outside? Such as:
Summary: Attributes that are named with a single underline or double underscore are just a convention, not that python must not be accessible. In fact, Python does not really prevent you from accessing private properties, which are also the same convention.
So, after the subclass inherits the parent class, does the subclass have access to the private properties of the parent class? See:
the third dimension of encapsulation : clear distinction between inside and outside, internal implementation logic, external unknown, and for encapsulation into the internal logic he provides a provider for external use (which is the real package), the meaning of encapsulation is to hide the properties inside the class, but what if the external to use it? This is the third level of encapsulation, we can define a function to call the double underscore property in the class category, so that we can indirectly access the properties hidden in the class by invoking this function. To rewrite the above example, see:
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()
Python_ object-oriented polymorphism, encapsulation