new animal subclasses;
Closed for modification: You do not need to modify functions such as run_twice () that depend on the animal type.
Inheritance can also be inherited from the first level, like from Grandpa to father, and then to the son of the relationship. And any class, in the end, can be traced back to the root class object, which looks like a backward tree. For example, the following inheritance tree:
Summary
Inheritance can take all the functions of the parent class directly, so that
data type. When we define a class, we actually define a data type. The data types we define are the same data types that python comes with, such as STR, list, dict:# A is a list type # B is the animal type # c is the dog typeDetermining whether a variable is a type can be judged by isinstance ():>>> isinstance (A, list) True>>> isinstance (b, Animal) True>>> Isinstance (c, Dog) TrueIt seems that a, B and C do correspond to the list, Animal, dog 3 ty
Polymorphism
Is a technology that allows you to set a parent object to be equal to one or more of its sub-objects, such as parent: = Child; polymorphism allows you to use the same class (base class) type pointer to reference objects of different classes, and perform the same operation in different ways according to different referenced objects.
The concept of
This article to share the content is about the Python object-oriented inheritance and polymorphism, has a certain reference value, the need for friends can refer to
In OOP programming, when we define a class, we can inherit from an existing class, the new class is called a subclass (subclass), and the inherited class is called the base class, the parent class, or the superclass (base classes, Supper Class)
relationship. And any class, in the end, can be traced back to the root class object, which looks like a backward tree. For example, the following inheritance tree: ┌───────────────┐ │ object │ └───────────────┘ │ ┌────────────┴────────────┐ │ │ ▼ ▼ ┌─────────────┐ ┌─────────────┐ │ Animal │ │ Plant │ └──
define are the same data types that python comes with, such as STR, list, dict:a = list() # a是list类型b = Animal() # b是Animal类型c = Dog() # c是Dog类型Determining whether a variable is a type can be judged by isinstance ():>>> isinstance(a, list)True>>> isinstance(b, Animal)True>>> isinstance(c, Dog)TrueIt seems that a, B and C do correspond to the list, Animal, dog 3 types.The Magic is:>>> isinstance(c, Animal)TrueIt seems that C is not just dog,c or anima
: The caller just calls, regardless of the details, and when we add a subclass of animal, make sure that the run () method is written correctly, regardless of how the original code was invoked. This is the famous "opening and closing" principle:
Open to expansion: Allow new animal subclasses;
Closed for modification: You do not need to modify functions such as run_twice () that depend on the animal type.
Inheritance can also be inherited at the first level, like a relationship from grandpa to
\Pyobject_head \py_ssize_t ob_size;It can be seen that the ob_size of this type is a description of its variable length characteristics (number of elements).Let's take a closer look at the contents of the Pyobject_head macro:_pyobject_head_extra: This macro expands either the front and back pointers of the doubly linked list or is empty, and is used when tracking all objects: Imagine a two-way list that concatenates all of Python's objects in a series of scenes, so grand!OB_REFCNT: This propert
#-*-coding:cp936-*-#python#Xiaodeng#polymorphism in Python#Polymorphism : The meaning of an operation depends on the type of object being manipulated, and the same message given to different objects can cause different actions. #polymorphism means that a variable does not kn
#three main features of object-oriented: encapsulation, inheritance, polymorphism#Impact of Inheritance: Resource inheritance, Resource usage, resource coverage, resource accumulation#The inheritance of resources, in Python, refers to resources that can use the parent class, rather than the subclass of the child class that also copies a copy of the parent class code into the subclass .classanimate:a= 1#Publ
Polymorphism is a basic feature of object-oriented language, and polymorphism means that variables do not know what the referenced object is, and behave differently depending on the object of reference. When dealing with polymorphic objects, you only need to focus on its interface, Python does not need to display the written (like Java) interface, in the use of o
This article mainly introduces the Python polymorphism and provides an in-depth analysis of the principles and implementation methods of Python polymorphism in Object-Oriented Programming in the form of examples, for more information about Python
Package EnclosureEncapsulation refers to the implementation details of a hidden class, which makes it easier for others to call.Purpose of Encapsulation:Allows the consumer to manipulate the object by indirectly invoking the method or property.So when Python uses encapsulation, it must use private properties and methods.Private properties and methods:A private member that starts with an ' __ ' double underscore and does not end with a double-declining
#python polymorphismclassDog ():defEat (self):Print("I am dog, eat something.")classCat ():defEat (self):Print("I am cat, eat somthing.")#polymorphic Invocation FormdefAnimal_eat (one): One.eat () d=Dog () C=Cat () animal_eat (d) animal_eat (c)" "python itself is a weakly typed language, and the variable itself has no type Python
What is polymorphic?Polymorphism refers to the presence of multiple states in a class, implemented through inheritance.Performance in Java: In a parameter you need to specify the data type, if this place can receive more than two types of parameters, then these types should have a parent class, the parent class is the type of all child class objects.Polymorphic Forms of Expression:def Pass def Pass def PassIn Pyth
types that python comes with, such as STR, list, dict:a = list() # a是list类型b = Animal() # b是Animal类型c = Dog() # c是Dog类型Judging whether a variable is a type can be isinstance() judged by:>>> isinstance(a, list)True>>> isinstance(b, Animal)True>>> isinstance(c, Dog)TrueIt seems a , b and indeed corresponds to, c list Animal Dog these 3 kinds.But wait, try it:>>> isinstance(c, Animal)TrueIt c seems Dog to be more than just, c still Animal !But think abo
knowledge, that is, Python itself is to support polymorphism, what is the benefit of doing so?1. Increased flexibility of the programStatus quo, regardless of the ever-changing object, the user is the same form to invoke, such as Func (File)2. Increased scalability of the programBy inheriting the File class, a new class is created, and the user does not have to change their own code or use func (file) to i
() overrides the parent class, and the subclass is run() always called when the code is running run() . In this way, we gain another benefit of inheritance: polymorphism.3 When we define a class, we actually define a data type. The data types we define are the same as the data types that python comes with, such as STR, list, Dict. Judging whether a variable is a type can be isinstance() judged.4 in an inher
Dynamic language polymorphism and static language C + + and other polymorphic meanings are not the same, C + + polymorphism is the number of parameters and types of different methods are different methods, and dynamic language in the real value of the method is the search process, that is, to find the right class (or a single-piece class), in the class can not find the words to find the parent Always found
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.