Introduction to Object-oriented technology
- 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.
- Class variables: class variables are common throughout the instantiated object. Class variables are defined in the class and outside the body of the function. Class variables are not typically used as instance variables.
- data members: class variables or instance variables are used to manipulate the data related to the class and its instance objects.
- method Overrides: if the method inherited from the parent class does not meet the requirements of the subclass, it can be overridden, which is called the override of the method, also known as the override of the method.
- instance variable: A variable defined in a method that acts only on the class of the current instance.
- inheritance: A derived class (derived class) that inherits the fields and methods of the base class. Inheritance also allows the object of a derived class to be treated as a base class object. For example, there is a design where an object of type dog is derived from the animal class, which is the analog "is a (is-a)" Relationship (example, dog is a animal).
- instantiation: Creates an instance of a class, the concrete object of the class.
- method: a function defined in a class.
- object: An instance of a data structure defined by a class. The object consists of two data members (class variables and instance variables) and methods.
In contrast to other programming languages, Python joins the class mechanism without adding new syntax and semantics.
The classes in Python provide all the basic functions of object-oriented programming: The inheritance mechanism of classes allows for multiple base classes, and derived classes can overwrite any method in the base class, and the method can be called in the base class with the same name.
An object can contain any number and type of data.
Class definition
class ClassName: <statement-1> ... <statement-N>
Class object
The class object supports two operations: Property Reference and instantiation.
The property reference uses the same standard syntax as all property references in Python: obj.name
After the class object is created, all the names in the class namespace are valid property names.
Many classes tend to create objects with an initial state. Therefore the class may define a special method named _init_ ()
def __init__ (self): = []
class defines the _init_ () method, the instantiation operation of the class automatically calls the _init_ () method similar to the default constructor in C + +
The use of self
There is only one special difference between a method of a class and a normal function, which is that it must have an extra first parameter name, by convention, its name is self
Methods of the class
Inside the class, you define a method using the DEF keyword, which differs from the general function definition in that the method of the class must contain the parameter self, and is the first argument, and self represents an instance of the class
class Test: def test (self, Parameter name 1, parameter name 2 ...)
Inherited
Python also supports inheritance of classes, as defined by
class Derivedclassname (BaseClassName1): <statement-1> ... <statement-n>,
Class properties and methods
Private properties of the class
__prvate_attrs: Two underscores begin with, declaring that the property is a private property and cannot be used outside of the class. Internal access to the class Self.__private_attrs
Methods of the class
Inside the class, define a method using the DEF keyword, which differs from the general function definition in that the method of the class must contain self and the first argument self represents an instance of the class
Private methods of the class
__private_method: Two underscore starts, you can also use this, declares that the method is private, can only be called internally, cannot be used outside of the class
Python Object-oriented