http://blog.csdn.net/wklken/article/details/6313265
First, class definition:
Class < category name:
< statements >
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:
[Python]View Plain Copy
- #类定义
- 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))
- p = people (' Tom ', 10,30)
- P.speak ()
Second, the Inheritance class definition:
1. Single Inheritance
Class < category name > (parent class name)
< statements >
eg.
Class Childbook (book)
Age = 10
[Python]View Plain Copy
- #单继承示例
- Class student (people):
- Grade = ' '
- def __init__ (self,n,a,w,g):
- #调用父类的构函
- people.__init__ (SELF,N,A,W)
- Self.grade = g
- #覆写父类的方法
- def speak (self):
- Print ("%s is speaking:i am%d years old,and I am in grade%d"% (Self.name,self.age,self.grade))
- s = Student (' Ken ', 20,60,3)
- S.speak ()
2. Multiple Inheritance of classes
Class name (parent Class 1, parent Class 2,...., parent class N)
< statement 1>
Note the order of the parent class in parentheses, if the same method name is in the parent class and is not specified when the subclass is used, Python searches from left to right
If the method is not found in the subclass, look from left to right to see if the method is included in the parent class
[Python]View Plain Copy
- #另一个类, the preparation before multiple inheritance
- 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)
- Test = sample ("Tim", 25,80,4, "Python")
- Test.speak () #方法名同, the default is to call the method in parentheses before the parent class
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 A
L = A. Class ()
Results of implementation of the above three procedures:
Python Notes-Class definitions