Copy CodeThe code is as follows:
Class Account (object):
"A simple Class"
Account_type= "Basic"
def __init__ (self,name,balance):
"Initialize 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
Where the __init__ function is the constructor in Python. In addition, the balance variable is a variable of the class instance.
In addition, defining member functions in a class in Python is usually the first argument always self, which means that the instance is the same as the this pointer in C + +, but the this pointer in C + + is implicitly and globally visible, but in Python it is passed as a parameter. This is another feature of defining classes in Python.
There is also a feature in the member function of the class, the use of another member function in the class, the preceding must specify the class name, as follows:
Copy CodeThe code is as follows:
Class Foo (object):
def bar (self):
Print "bar!"
def spam (self):
Bar (self) # error, raised Nameerror, can be: Self.bar
Foo.bar (self) # Legal
2. Sound-time static methods in classes and using static methods
To use a static method in a class, precede the class member function with the @staticmethod tag to indicate that the following member function is a static function. The advantage of using static methods is that you do not need to define an instance to use this method: In addition, multiple instances share this static method, as follows:
Copy CodeThe code is as follows:
Class Simclass ():
@staticmethod
Def sharestr ():
Print "This is a static Method"
Simclass.sharestr () #使用静态函数
3. Class method:
A class method differs from a normal member function and a static function, and it seems that the semantics are not seen in the language in which it is defined:
A class method can be called by a class or its instance, whether you call this method with a class or a class instance, and the first parameter of the method always defines the method's class object.
Remember that the first parameter of a method is a class object and not an instance object.
By convention, the first formal parameter of a class method is named CLS. It is not necessary to define a class method at any time (a function that the class method can implement can be implemented by defining a common function, as long as the function accepts a class object as a parameter).
Define class methods and use class methods:
Copy CodeThe code is as follows:
Class Abase (object):
@classmethod #类方法修饰符
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
In other words, the class method is not necessary, and the function of the class method can be implemented using the normal function.
4. Inheritance of Classes
In Python, you inherit a class, like this:
Class A (object) #继承object类
#.......
Class B (A) #继承A类
#........
In addition, Python supports multiple inheritance, for multiple inheritance, to find a corresponding function, its python has a corresponding method, such as:
Copy CodeThe code is as follows:
Class D (oject): Pass #D继承自object
Class B (D): #B是D的子类
Varb = 42
def method1 (self):
Print "Class b:method1"
Class C (D): #C也是D的子类
Varc = 37
def method1 (self):
Print "Class c:method1"
def method2 (self):
Print "Class c:method2"
Class A (b,c): #A是B和C的子类
VarA = 3.3
def method3 (self):
Print "Class a:method3"
What happens if I want to call A.METHOD1 ()? The answer is CLASSB:METHOD1. The book is just a sample introduction:
When searching for a property defined in the base class, Python takes the principle of depth precedence and searches in the order of the base classes in the subclass definition. * * NOTE * * (the New-style class has changed this behavior). In the example above, if you access A.varb, you will search in the order of a-b-d-c-d, and stop searching as soon as you find it. If there are multiple base classes that define the same property, only the first found property value is used:
5. Data hiding
Data hiding in Python is very simple, do not need to add a keyword in front, as long as the class variable name or member function before adding two underscore the function of data hiding, so, for the class instance, its variable name and member function is not used, for its class inheritance class, is also hidden, so , its inheriting class can define its exact variable name or member function name without causing a naming conflict.
Copy CodeThe code is as follows:
Class A:
def __init__ (self):
self.__x = 3 # self._a__x
Class B (A):
def __init__ (self):
A.__init__ (self)
self.__x = Notoginseng # self._b__x