Python class definition and class inheritance

Source: Internet
Author: User
first, class definition:

Class 
 
  <类名>
   
  :  
  
   <语句>
 
  

After a class is instantiated, its properties can be used, and in fact, after a class is created, its properties can be accessed through the class name
If you modify its properties directly using the class name, it will directly affect the object that has already been instantiated

Private properties of the class:
__private_attrs two underscores, declares that the property is private and cannot be used outside of the class or accessed directly
When used in methods inside a class Self.__private_attrs

Methods of the class
Inside the class, using the DEF keyword, you can define a method for a class that differs from a generic function definition, which must contain the parameter self, and is the first argument


Private class methods
__private_method two underscores, declares that the method is a private method and cannot be called outside of a class
Calling Slef.__private_methods inside a class
The proprietary methods of the class:
The __init__ constructor, which is called when the object is generated
__del__ destructors, using when releasing objects
__repr__ Printing, converting
__setitem__ Assignment by index
__getitem__ getting values by index
__len__ Get length
__CMP__ comparison operations
__call__ function call

__add__ Plus arithmetic
__SUB__ Reduction Operations
__mul__ Multiply operation
__DIV__ in addition to operations
__mod__ Calculating the remainder
__pow__ said Fang
Example:

#类定义 class people:   #定义基本属性   name = ' Age   = 0   #定义私有属性, private properties cannot be accessed directly outside the class   __weight = 0   #定义构造方法   def __init__ (self,n,a,w):     self.name = n     self.age = a     self.__weight = w   def speak (self):     Print ("%s is speaking:i am%d years old"% (self.name,self.age))   

Second, the Inheritance class definition:

1. Single Inheritance

Class 
 
  <类名>
   
  (parent class name)  
  
   <语句>
    
   eg.class childbook (book) Age  = ten
  
   
 
  

2. Multiple Inheritance of classes

Class name (parent Class 1, parent Class 2,...., parent class N)   
 
  <语句1>


Note the order of the parent class in the parentheses, if the parent class has the same method name, and if the subclass is not specified when it is used, Python searches from left to right, that is, when the method is not found in the subclass, the parent class is searched from left to right to include the method

#另一个类, the preparation of multiple inheritance before class speaker ():   topic = '   name = '   def __init__ (self,n,t):     self.name = n     Self.topic = T   def speak (self):     print ("I am%s,i am a speaker! My topic is%s "% (self.name,self.topic))  #多重继承 class sample (Speaker,student):   a = '   def __init__ (Self,n,a, w,g,t):     student.__init__ (self,n,a,w,g)     speaker.__init__ (self,n,t)  

Overriding of class methods-methods for subclasses covering out parent classes
Def method name is consistent with parent class
If you want to use the parent class name in the method to the parent class method. Method Name

If you put the class in the module

When used

Import Al = A. Class ()

Results of implementation of the above three procedures:

  • 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.