Explanation of the Python class definition _python

Source: Internet
Author: User
Tags class definition function definition
First, class definition:
Copy Code code as follows:

Class < category name:
< statements >

class, you can use its properties, and in fact, after you create a class, you can access its properties 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 begins with a two underscore, declaring that the property is private and cannot be used or accessed directly outside the class. When used in methods inside a class Self.__private_attrs
Methods of the class
Within the class, use the DEF keyword to define a method for the class, unlike a generic function definition, the class method must contain the parameter self and the first argument
Private class method
__private_method begins with a two underscore, declares that the method is a private method and cannot be invoked outside of the class. Calling Slef.__private_methods inside a class

The proprietary method of the class:
__init__ constructor, called when the object is generated
__del__ destructor, use when releasing objects
__repr__ Printing, converting
__SETITEM__ is assigned by index
__GETITEM__ Get values by index
__len__ Get length
__CMP__ comparison operations
__call__ function call

__add__ addition operation
__sub__ Subtraction Operation
__mul__ multiplication operation
__DIV__ in addition to operations
__MOD__ calculation
__pow__ said the Square
Copy Code code as follows:

#类定义
class people:
#定义基本属性
Name = '
Age = 0
#定义私有属性, private properties cannot be accessed directly outside of a 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))


p = people (' Tom ', 10,30)
P.speak ()

second, the Inheritance class definition:
1. Single Inheritance
Copy Code code as follows:

Class < classes name > (parent class name)
< statements >

Copy Code code as follows:

Class Childbook (book)
Age = 10

Copy Code code as follows:

#单继承示例  
    class student (people): 
& nbsp;       grade = '  
        def __init __ (self,n,a,w,g): 
            #调用父类的构函  
            people.__init__ (self,n,a,w)  
            Self.grade = g 
         #覆写父类的方法  
        def speak (self): 
& nbsp;           print ('%s is speaking:i am%d years old,and I am ' in GRA De%d "% (Self.name,self.age,self.grade))       

    s = student ( ' Ken ', 20,60,3  
    s.speak ()

2. Multiple Inheritance of classes
Copy Code code as follows:

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

Note the order of the parent class in the parentheses, if the parent class has the same method name and is not specified when the subclass is used, Python searches from left to right, that is, if the method is not found in the subclass, find the method in the parent class from left to right
Copy Code code as follows:

#另一个类, preparation before multiple inheritance  
Class speaker (): 
    Topic = '  
    name = '  
    def __init__ (self,n,t): 
&nb sp;       self.name = n 
        Self.topic = t 
    def speak (self): 
        pri NT ("I am%s,i am a speaker!") My topic is%s% (self.name,self.topic)  

#多重继承  
Class sample (Speaker,student): 
&nb sp;   a = '  
    def __init__ (self,n,a,w,g,t): 
    & nbsp;   student.__init__ (self,n,a,w,g)  
        speaker.__ init__ (self,n,t)  

Test = sample ("Tim", 25,80,4, "Python")  
Test.speak () #方法名同, The default invocation is the method
of the parent class that is queued in parentheses.
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.