Python object-oriented-inheritance

Source: Internet
Author: User

#coding: Utf-8 "" "Term: 1, in Python3, custom-created classes are inherited by default in the Python class named Object Class 2, the object class provides methods, is a double underscore, is to provide for Python internal use 3, The inherited class is called a superclass, or a class called the parent class 4, inherited from the superclass (parent class) is called Subclass 5, the subclass derives from the parent class, or the subclass extends the function of the parent class "" "" #扩展类: Add a new behavior (method) Class util to an already existing class:   #我定义的这个类 (intended to be a tool class), does not explicitly inherit who, so it inherits the object class by default      #定义扫描ip范围的方法     def  scan_ip_range (Self, ip_range):         print ("start  scan ip range:  ",  ip_range) Class childutil (Util):  #这个类继承了Util类       #添加一个扫描端口的功能     def scan_port (self, port):         print ("start scan port: ",  port) if __name__ ==  ' __main__ ' :     s = childutil ()   #实例化子类后, creating an object S    s.scan_ip_range ("10.36.1.0/24")   #扫描IP范围的方法是从父类继承过来的     s.scan_port   #扫描端口的方法是在子类中扩展的 "" "" "" # Overriding common methods # 1, rewriting, such as having a party in the parent classmethod, but not satisfied with the A method, the need to rewrite # 2, the solution is to write a subclass in the same name as in the parent Class A method, the name is also called a# 3, so that is equivalent to rewriting the Class supper ():     def a (self):         print ("Hello a") class  child (Supper):     def a (self):         Print ("Hi! a ...") if __name__ ==  ' __main__ ':     o = child ()     O.A ()   #此刻的调用是子类中的a方法      #如果把子类中的a方法去掉, then the call is a method in the parent class      #  Test Conclusion: The A method in     # 1, subclass only overrides the A method in the parent class, so the A method in the parent class still exists      # 2, if the subclass does not have a method, it will be found in the parent class, the parent class will not be found in the object class, or the exception is thrown     # 3, therefore, It can be seen that this lookup order is the nearest principle "" "#重写__init__ (), the normal method can be rewritten, initialization can also be written from" "" For example: there is a class to describe the physical machine object, and the object has a host name, IP address, The 3 basic properties of the operating system also have a class that describes the virtual machine object for VMware, and 3 properties like the physical machine, but the virtual machine also has a Vmtools property. So, when defining a virtual machine class, do you want to redefine the three properties of hostname, IP address, operating system? Now that the virtual machine class has a vmtools attribute, there must be an initialization method, but the class that describes the physical machine object also has an initialization method.Problem, the solution code is as follows "" "" "" Class host:    def __init__ (self, hostname, ip,  OS):        self.hostname = hostname         self.ip = ip        self.os =  os    def get_info (self):         print ( Self.hostname, self.ip, self.os) CLASS VM (Host):     def __init__ (Self,  hostname, ip, os, vmtools):         super (). __init __ (hostname, ip, os)   #super的功能就是调用父类的代码          self.vmtools = vmtools     #重写了父类的get_info方法     def get_ info (self):         super (). Get_info ()   #super的功能就是调用父类的代码          print (self.vmtools, ) if __name__ ==  ' __main__ ':     v  = VM ("Vm1", "10.2.3.4", "centos6.5",  "Vmtools_v1")     v.get_info () "" "#多重继承" " "There is a parent class A, a parent class B, a subclass C, but Class C has no Class C at the same time inherit the parent class A and B, when C has the parent class A and B properties and methods, are inherited" "" "class a:     Def get_a (self):         print ("My a ...") class B:     def get_b (self):         print ("My b ...") Class c (a, b):  #继承A和B     passif __name__ ==  ' __main__ ':     c = c ()     c.get_a ()     c.get_b ()      #应用场景一目了然, needless to say what       #在查找顺序, first in Class C to find, then, then is a something else in B to find? This involves a way to find the problem       #多重继承, there are two ways to search, namely depth first and breadth first, specific Google


Python object-oriented-inheritance

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.