Alibabacloud.com offers a wide variety of articles about python class inheritance init, easily find your python class inheritance init information here online.
The Python programming language is a powerful development language. Its biggest feature is its ease of use. It also has object-oriented features, which can help us implement some specific functional requirements. We will introduce the concept of Python inheritance in detail here today.
In-depth exploration of the magic of the
Declaration of the class:I. Properties of a class(Private and public properties)(Class Property)Ii. methods of the class(Construction method, destructor method, custom method, special member method)(Static methods, class methods, class properties)Iii.
# This is a learning note for the Liaoche teacher Python tutorialA child class can have more than one parent class. This is called multiple inheritance. with multiple inheritance, a subclass can get all the functionality of multiple parent classes at the same time. 1.1 , M i
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
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
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
class is not callable
# destructors for the base class are not automatically called
print (' High destruct ')
# Inheriting a method of a class overrides the method with the same name as the base class
def Test (self):
print (' High Test ')
# Python does
, it is the beginning of the __ to hide a property.Supplemental Note: Encapsulation is definitely not a pure meaning of concealmentThe purpose of defining the attribute is to let the user use it, and the user wants to use the property hidden within the class.The designer of the class needs to open an interface within the class (define a method) to access the hidden properties inside the method, the user wil
' Enter F ' super (F, self). __ Init__ () # change print "Leave F"
f = f (), execution result:
Enter F Enter e enter B Enter C Enter D Enter a leave a leave D leave C leave B leave E leave FAs can be seen, the initialization of F not only completes all calls to the parent class, but also guarantees that the initialization function of each parent class is called only once.
Summary
1. Super is
, 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 si
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 o
Class (1) and Python class (Class (1) in Python I. Application scenarios
If multiple functions have the same parameters, convert them to object-oriented.Ii. How to Create a class
Class
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
1Python 2inheritance in. 72In Python 2. 7, the inheritance syntax is slightly different, and the definition of the Electriccar class is similar to the following:3 classCar (object):4 def __init__(self, make, model,year):5--snip--6 7 classElectriccar (Car):8 def __init__(self, make, model,year):9 Super (Electriccar, self). __init__ (make, model, ye
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 cla
Objective:Inheritance is one of the three main features of object-oriented, so you should pay attention to 3 points for inheritance.first, the basic searchIf the subclass inherits the parent class, the child class instantiates the object, and no methods and properties are found to the parent class.class # Parent Class def F1 (self): Print ('F1')
facing Objects1. Encapsulation#What is the encapsulation in Python? #use the construction method to encapsulate the content into an object, and then indirectly obtain the encapsulated content through the object directly or self; classOop (object):def __init__(self): Self.name=name Self.age= Agedefget_attrs (self):Print(Self.name)Print(self.age) obj= Oop ('Mic', 18) #Direct Access Print(Obj.name, Obj.age)#Indirect AcquisitionObj.get_attrs (
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 t
It's not long enough to touch the Python language, and many of the features of the language are not well understood, and many usages are not yet known. Today I want to write a function call in the inheritance of Python object-oriented programming. Share it and make progress together.Because of previous exposure to Java and C + +, all of the ideas for object-orien
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
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.