In this article let's look at the knowledge about classes, some friends may have just come into contact with Python, the programming language, for what is
what does the Python class mean?I don't understand, but it's okay. The next article will take you to learn what "class" is.
I. Definition of Python class
Class: Used to describe a collection of objects that have the same properties and methods. It defines the properties and methods that are common to each object in the collection. An object is an instance of a class.
Two. How to create a class
Use the class statement to create a new class, followed by the name of the class and ending with a colon:
Class ClassName: ' Help information for classes ' #类文档字符串 class_suite #类体
The Help information for the class can be viewed through classname.__doc__.
Class_suite consists of class members, methods, and data properties.
Three. Python creates class instances
The following is an example of a simple Python class:
#!/usr/bin/python#-*-Coding:utf-8-*-class Employee: ' base class for all employees ' empcount = 0 def __init__ (self, name, Sala RY): self.name = name self.salary = Salary Employee.empcount + = 1 def displaycount (self): print " Total Employee%d '% employee.empcount def displayemployee (self): print "Name:", Self.name, ", Salary:", S Elf.salary
(The Empcount variable is a class variable whose value is shared across all instances of the class.) You can use Employee.empcount access in internal classes or outside classes.
The first method, the __init__ () method, is a special method called the constructor or initialization method of a class that is called when an instance of the class is created.
Self represents an instance of a class, and self is necessary when defining a method of a class, although you do not have to pass in the appropriate parameters when calling. )