Inheritance is a way of creating new classes in Python, where a new class can inherit one or more parent classes, which can be called a base class or a superclass, and a new class is called a derived class or subclass.The inheritance of classes in Python is divided into: single inh
() 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
, you still have to define a self parameter for this method.The __init__ method in a class is passed to the __init__ method when it creates a new instance of a class, including the parameters in parentheses followed by the class name. It runs immediately when an object of the class is created. The __init__ method is similar to constructors in C + +, C #, and Java, and can be used to assign values to properties. Like the __init__ method, there is a special method __del__, which is called when the
: 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 fir
This article mainly introduces python multi-inheritance instances, which are simple, practical, and easy to understand. For more information about the usage of python multi-inheritance, see the examples in this article. The specific implementation method is as follows:
1. the mro. py file is as follows:
#! /Usr/b
father may use in business negotiations, you may want to use on the girls). Metaphor may not be very image, please forgive me.(1) Create a class in an inherited wayclass Peoplelist (list):def __init__ (self):LIST.__INIT__ ([])Peoplelist (list) The new class derives from the list class, list.__init__ ([]) initializes the derived class(2) For example, the following class can inherit all the methods of the listPeoplelist (list):__init__ (self,name,date=None, achievement=[]):LIST.__INIT__ ([])Self.
This example describes the Python class inheritance usage. Share to everyone for your reference. Here's how:
#!/usr/bin/python# Filename:inherit.pyclass Schoolmember: "represents any school member." def __init__ (self, Name, age): Self.name = name Self.age = Age print ' (Initialized schoolmember:%s) '% Self.name def tell: "Tell my details." print ' Name: '
This article mainly introduces the new python multi-inheritance algorithm C3, which requires complex algorithms. This article describes in detail the new algorithm C3, for more information, see mro (method resolution order). It is mainly used to determine the path of an attribute (from which class) when multiple inheritance occurs ).
In python2.2, the basic idea
There is nothing to say, inheritance is mainly inherited from the parent class of some methods, the code is very detailed#!/usr/bin/env python #coding: Utf-8class Father (object): #新式类 def __init__ (self): self.name= ' Liu ' self . Familyname= ' Yan ' def Lee (self): print ' I am the parent function Lee ' def Allen (self): print "I am the parent function Alle
MRO is the method Resolution order, which is proposed mainly to solve the python in multiple inheritance, when the parent class has the same name function, the ambiguity of the problemLet's look at an example below: import Inspect class D: pass class C (D): pass class B (D): pass class A (B, C): pass if __name__ = = " __main__ " : print (Inspect.getmro (A)) b and C inherit d a i
, like C + +, supports multiple inheritance. Although the concept is easy, the hard work is that if a subclass invokes a property that itself does not have a definition, it is in what order to find it in the parent class, especially if many of the parent classes contain the same name attribute.For classic and modern classes, the order in which attributes are found is different. Now let's look at two different manifestations of the classic and new clas
Python is an object-oriented language, so OOP programming is a must.Below, I will summarize my study experience and learn knowledge.1. Declaration of a classClass Student (object):PassClass is the keyword that declares the class, and Student is the class name. In parentheses, object is used for inheritance, and if no other class is inherited, the object class is inherited.The pass area is the method and pro
# static resource file Staticfiles _dirs = (Os.path.join (base_dir, "statics"),) # Now add the configuration, here is the tuple, note the commaTemplates/base.htmlTemplates/order.html{% extends "base.html"%} {# This inherits the base class and must be placed in the first row #{% block content%} {# Fill in the new content according to the name of the Block #} Templates/shopping.html{% extends "base.html"%} {# This inherits the base class and must be placed in the first line #} {% blo
InheritedThe implication of inheritance is that subclasses inherit the namespace of the parent class, which can invoke the properties and methods of the parent class, and because of the way the namespace is found, when the child class defines the property or method with the same name as the parent class, the instance of the subclass calls the attribute in the subclass, not the parent class, which forms the polymorphism in Python:Def superclass:? ? def
Inheritance is designed for code reuse and design reuseIn an inheritance relationship, an existing, well-designed class is called a parent class or base class, and a newly designed class is a subclass or a derived classDerived classes can inherit the public members of the parent class, but cannot inherit their private membersIf you need to call a method of a base class in a derived class, you can use the bu
Python class inheritance and polymorphismIn OOP (Object oriented programming) programming, when we define a class, we can inherit from an existing class, a new class called a subclass (subclass), The inherited class is called the base class, the parent class, or the superclass (base class, Super Class).Let's start by defining a class, named person, representing people, defining attribute variables name and
The classes in object-oriented programming have three main features: inheritance , encapsulation , polymorphisminheritance : creating specialized class objects based on common classesEncapsulation : working details of hidden objects in the external world polymorphic: You can use the same action for objects of different classesThe basic form of a class's inheritance definition in
related side, this time the sequence is {D,C1}
Next, the vertex with a degree of 0 has two (A,C1), according to the leftmost principle, take a, cut off a related side, this time the sequence is {d,c1,a}
Then see, the vertex of the degree of 0 is C2, take C2, cut off C2 related side, this time the sequence is {D,C1,A,C2}
To continue, the vertex with a degree of 0 is B, take B, cut off the related side of B, and finally an object
So the last sequence is {d,c1,a,c2,b,object}
F
Python supports multiple inheritance, and as with C + + There is a problem: multiple parent classes inheriting by subclasses inherit the same parent class, and it is possible that the parent class constructor method is called multiple times. On this issue, I have found some information, although not personally fully verified, here I summarize my views on this issue.Python and C + + have a different solution
Concept: A class can have more than one parent class
Example:
classA:defEat (self):Print('eat func of A') classB:defEat (self):Print('eat func of B') #Multiple inheritance, separated by a comma from several parent classesclassC (B, A):defEat (self):#When multiple parent classes have the same method, they are selected in the order in which they are inherited #super (). Eat () #If you do not want t
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.