Class Definition
The code is as follows:
Class Account (object ):
"A simple class"
Account_type = "Basic"
Def _ init _ (self, name, balance ):
"Initializing a new Account instance"
Self. name = name
Self. balance = balance
Def deposit (self, amt ):
"Deposit"
Self. balance = self. balance + amt
Def withdraw (self, amt ):
"Withdrawal"
Self. balance = self. balance-amt
Def inquiry (self ):
"Return Current Balance"
Return self. balance
The __init _ function is the constructor in Python. In addition, the balance variable is a variable of the class instance.
In addition, in python, the first parameter for defining a member function is always self, indicating its own instance, which is similar to the this pointer in C ++, however, the this pointer in C ++ is invisible and globally visible, but must be passed as a parameter in Python. this is another feature of defining classes in Python.
Another feature is that when using another member function in the class, you must specify the class name as follows:
The code is as follows:
Class Foo (object ):
Def bar (self ):
Print "bar! "
Def spam (self ):
Bar (self) # The NameError is thrown, which can be self. bar.
Foo. bar (self) # valid
2. static method in class sound and use static method
To use static methods in a class, add the @ staticmethod mark before the class member function to indicate that the following member functions are static functions. The advantage of using the static method is that you can use this method without defining an instance: In addition, multiple instances share this static method as follows:
The code is as follows:
Class SimClass ():
@ Staticmethod
Def ShareStr ():
Print "This is a static Method"
SimClass. ShareStr () # use static functions
3. class methods:
The class method is different from the common member functions and static functions. in the language of contact, it seems that you have never seen such semantics. let's look at its definition:
A class method can be called through a class or its instance. Whether you use a class to call this method or a class instance to call this method, the first parameter of the method always defines the class object of the method.
Remember: the first parameter of a method is a class object rather than an instance object.
By convention, the first parameter of the class method is named cls. defining class methods is not necessary at any time (all functions that can be implemented by class methods can be implemented by defining a common function, as long as this function accepts a class object as a parameter ).
Define the class method and use the class method:
The code is as follows:
Class ABase (object ):
@ Classmethod # class method modifier
Def aclassmet (cls): print 'a class method for ', cls. _ name __
Class ADeriv (ABase): pass
BInstance = ABase ()
DInstance = ADeriv ()
ABase. aclassmet () # prints: a class method for ABase
BInstance. aclassmet () # prints: a class method for ABase
ADeriv. aclassmet () # prints: a class method for ADeriv
DInstance. aclassmet () # prints: a class method for ADeriv
That is to say, class methods are not necessary. using common functions can also implement the functions of class methods.
4. class inheritance
In python, inherit a class like this:
Class A (object) # inherit the object class
#.......
Class B (A) # inherit class
#........
In addition, python supports multi-inheritance. for multi-inheritance, find a corresponding function. python has corresponding methods, such:
The code is as follows:
Class D (oject): pass # D inherits from object
Class B (D): # B is a subclass of D.
VarB = 42
Def method1 (self ):
Print "Class B: method1"
Class C (D): # C is also a subclass of D.
VarC = 37
Def method1 (self ):
Print "Class C: method1"
Def method2 (self ):
Print "Class C: method2"
Class A (B, C): # A is A subclass of B and C.
VarA = 3.3
Def method3 (self ):
Print "Class A: method3"
What will happen if I want to call A. method1? The answer is ClassB: method1. the book only introduces the following:
When you search for an attribute defined in the base class, Python uses the depth-first principle and searches by the base class sequence defined in the subclass. ** Note ** (the new-style class has changed this behavior ). In the preceding example. varB, the search will be in the order of A-B-D-C-D, as long as the search is found to stop. if multiple base classes define the same attribute, only the first property value to be found is used:
5. Data Hiding
It is easy to implement data hiding in python. you do not need to add any keywords in front of the class variable name or member function. you only need to add two underscores in front of the class variable name or member function to implement the data hiding function, for an instance of a class, its variable name and member function cannot be used, and its inherited class is also hidden, the inherited class can define the same variable name or member function name without causing name conflict.
The code is as follows:
Class:
Def _ init _ (self ):
Self. _ X = 3 # self. _ a1_x
Class B ():
Def _ init _ (self ):
A. _ init _ (self)
Self. _ X = 37 # self. _ b1_x