Python study notes (class)

In the object-oriented method, the definition of a class is: a class is a collection of objects with the same attributes and service functions, which provides a unified abstract description for all objects of the class, which includes two main attributes and services Partly, in an object-oriented programming language, a class is an independent program unit. It should have a class name and include two main parts: attribute (data) definition and behavior definition.
(1) Class and object
The relationship between class and object is like the relationship between a mold and castings cast with this mold. A class gives an abstract definition of all objects belonging to that class, and an object is an entity that meets this definition. Therefore, an object is also called an instance of a class.
(2) Classes, attributes and methods
① The abstraction of objects with the same type of attributes is a class.
② Classes have specific behaviors (methods) in addition to specific attributes.
③ Create new species (class): encapsulate attributes and methods together.
(3) Three characteristics of class
Encapsulation : Encapsulation has two meanings. The first meaning is to combine all attributes and all behaviors of an object to form an inseparable independent unit (ie object); This meaning is also called "information hiding". That is to hide the internal details of the object as much as possible, form a boundary (or form a barrier) to the outside, and only retain a limited external interface to make it contact with the outside.
Inheritance : You can set out to define a new class from an existing class, the new class is defined as a subclass. Subclasses inherit the variables and methods of the parent class, and have their own variables and methods. The benefit of inheritance is reduced code reuse.
Polymorphism : After the attributes or behaviors defined in the parent class are inherited by the subclass, they can have different data types or exhibit different behaviors. This allows the same attribute or behavior to have different semantics in the parent class and its various subclasses.

Published 48 original articles · Like 25 · Visit 2453

Guess you like

Origin blog.csdn.net/qq_43628959/article/details/99859271