Inheritance such as Python learning

Source: Internet
Author: User

An important feature of object-oriented is inheritance, and the benefit of inheritance is to improve the reuse rate of code and reduce unnecessary code. Inheritance is the relationship of the parent class to the child class, and when the subclass inherits the parent class, it has all the variables and methods of the parent class. The syntax for defining inheritance in Python is: Classderived classes name (base class name). There are a few things to consider when using Python inheritance:

(1) When the parent class defines the __init__ () initialization method, the subclass is not called automatically, but we need to show the call, and if we want to extend the parent class's variable, we can add the parameter in __init__ ().

(2) when calling a method of a base class, you need to prefix the class name of the base class with the Self argument variable.

(3) Python always finds the method of the corresponding type first, and if it cannot find the corresponding method in the derived class, it begins to look up one by one in the base class.

Examples Show

Parent class

#BaseModleclass baseclass:def __init__ (self,name,age): Self.name=name self.age=age print ("Basecl Inited ") def Speak (Self,sth): Print (" BaseClass is speaking:%s "%sth); if (__name__== ' __main__ '): Print ( "Run by Myself") BaseClass () else:print ("I am import module")

‘‘‘

__name__ property in Python: It means that when we execute this module, we see whether it is executed independently by the user or after import, for example, in this example, we execute the derived module Submodule,basemodulde is the imported module. The I am import module is therefore output.

Each Python module has its __name__, if it is ' __main__ ', which means that the module is run by the user individually, we can do the appropriate operation.

‘‘‘

Derived classes

#SubModuleimport Basemoduleclass Subclass (Basemodule.baseclass): #需要加上模块名, otherwise error def __init__ (self,name,age,salary): basemodule.baseclass.__init__ (Self,name,age) #显示调用父类的初始化函数, and expands the variable self.salary=salary print ("Subclass is Init Ed ") def Talk (self, STH): Print ("%s is talking%s "% (self.name,sth)) BaseModule.BaseClass.speak (self,sth) # You need to specify the module name when calling the parent class method. Parent class Name if (__name__== "__main__"): Print ("Run by Myself") S=subclass ("yping", 25,1000) S.talk ("a Story")

Run results

>>> I am import Modulerun by Myselfbaseclass are Initedsubclass is initedyping are talking a storybaseclass is Spea King:a story>>>

Results Analysis:

Here first output "I am import module", the program is first from (__name__= "__main__") judgment start, first perform the parent class (__name__= "__main__") judgment, because now is not directly execute Basemodule module , so the output "I am import module", then executes the subclass's entry function, instantiates a subclass class, first initializes the parent class, then initializes the derived class, executes the talk () function, and then calls the parent class speak () function.

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.