1, the creation and use of classes
- class creation
- class usage
(1), the creation of the class
The general form of a class is as follows:
class <类名>(父类名):
类中的函数等
Example of class definition:
class myClass:
pass
(2), the use of classes
After a class is defined, it needs to be instantiated before it can be used. After the class is instantiated, an instance of the class is generated ; a class can be instantiated
by adding parentheses to the class;
a class can be instantiated with multiple instances, and there is no difference between instances Influence each other.
Example of instantiation of a class:
class MyClass:
'MyClass Help'
myClass = MyClass()
print(myClass.__doc__)
help(myClass)
Running result:
MyClass Help
Help on MyClass in module main object:
class MyClass(builtins.object)
| MyClass Help
|
| Data descriptors defined here:
|
| dict
| dictionary for instance variables (if defined)
|
| weakref
| list of weak references to the object (if defined)
2. Class attributes and methods
- class method
- class properties
- class method vs static method
(1), class methods
- The first parameter in the method must be self and cannot be omitted;
- The method call needs to instantiate the class and call it in the form of instance name.method name;
- The overall indentation is one unit, indicating the content of the class;
Examples of methods in a class:
class MyClass :
def function1(self):
print('This is function1')
def function2(self):
print('This is function2')
myClass = MyClass()
myClass.function1()
myClass.function2()
Running result:
This is function1
This is function2
- When calling the method, you don't have to pass the self parameter;
- The __init__() method is used to initialize related data when the class is instantiated;
- When a method defined in this class is called from a class, the self parameter should not be passed in the parameter list of the call
class MyClass :
def __init__(self, x = 10, y = 20):
self.x = x
self.y = y
def calcAdd(self):
return self.getX() + self.getY()
def getX(self):
return self.x
def getY(self):
return self.y
myClass = MyClass(22, 30)
print(myClass.calcAdd())
Running result:
52
(2), the attributes of the class
In Python, the class class definition attribute is to use it directly first, and the attribute can be defined in the construction method; the defined attribute
can also be used in other methods in the class;
There are two types of class attributes in the python language, similar to member variables and static member variables in C++:
- Instance attribute: use instance name.attribute name when using
- Class attribute: use class name.attribute name when using
Examples of properties of a class:
class MyClass :
class_name = 'MyClass'
def __init__(self, x = 10, y = 20, name = 'MyClass'):
self.x = x
self.y = y
MyClass.class_name = name
def calcAdd(self):
return self.getX() + self.getY()
def getX(self):
return self.x
def getY(self):
return self.y
myClass = MyClass(22, 30, 'TestClass')
print(myClass.x)
print(MyClass.class_name)
Running result:
22
TestClass
(3), class method and static method
Class methods are of the following types:
- instance method
- class method
- static method
- Static methods are decorated with the decorator @staticmethod and have no default parameters;
- The class method is decorated with the decorator @classmethod and must have the default parameter "cls"
- When calling, no instantiation is required; directly use the class name. Method name () call
Examples of class methods and static methods:
class MyClass :
class_name = 'MyClass'
def __init__(self, x = 10, y = 20, name = 'MyClass'):
self.x = x
self.y = y
MyClass.class_name = name
# 类的静态方法
@staticmethod
def getClassName():
print(MyClass.class_name)
# 类方法
@classmethod
def getClassName2(cls):
print(MyClass.class_name)
MyClass.getClassName()
myClass = MyClass(22, 30, 'TestClass')
MyClass.getClassName2()
Running result:
MyClass
TestClass