6 Object-oriented programming

6.1 Definition and use of categories

  • The class keyword defines the class, followed by a space, followed by the class name,
  • If derived from other base classes
    • Put all base classes in a pair of brackets and separate them with commas,
    • Then there is a colon, and finally a new line and define the internal implementation of the class.
  • The first letter of the class name is generally capitalized

Insert picture description here

  • After defining the class, it can be used to instantiate the object,
  • And through the "object name. Member" to access the data members or member methods.

Insert picture description here

Insert picture description here

  • Python provides the keyword pass, nothing will happen when executed,
    • Used in the definition of classes and functions or in selection structures to indicate empty statements
  • If you are not sure how to implement a function,
    • Or reserve a little space in advance for future software upgrades,
    • Available pass "placeholder".

  • As with defining functions, when defining a class, you can also annotate the class with triple quotes

Insert picture description here

6.2 Data members and member methods

6.2.1 Private and public members

  • Private members cannot be accessed directly outside the class,
    • It is generally accessed and operated within the class,
    • Or access from outside the class by calling the public member method of the object,
  • Public members are publicly available,
    • It can be accessed inside the class or used in external programs.

  • If the member name starts with two (or more) underlines but does not end with two (or more) underlines, it means that it is a private member, otherwise it is not a private member.
  • "Object name._class name__xx" can also access private members in external programs,
    • But this will destroy the encapsulation of the class, it is not recommended

Insert picture description here

  • "." Is a member access operator, which can access members in a namespace, module, or object. In
    IDLE, Eclipse + Pydev, WINGIDE, Pycharm, or other Python environments, the object or class name is followed by ".", Which are automatically listed Out of all its public members
  • "." After adding an underline, then list all members of the object or class, including private members
  • You can also view all members of a specified object, module, or namespace with dir ().

Insert picture description here

  • An underline starts to protect members,
    • Only class objects and subclass objects can access these members,
    • Direct access is generally not recommended outside the class:
    • Members beginning with one or more underscores in a module cannot be imported from from module import * 'unless the __all__ variable is used in the module to explicitly indicate that such members can be imported
  • Two underlines before and after each, special members defined by the system
  • Start with two or more underlines but not end with two or more underlines, indicating private
    members, only the class object can access it, and subclass objects cannot access the member, but outside the object

6.2.2 Data members

  • The data members belonging to the object are defined in the constructor init (),
    • It can also be defined in other member methods,
    • Use self as a prefix when defining and accessing data members in instance methods
  • Data members belonging to a class are shared by all objects of the class, not belonging to any one object,
    • When defining a class, such data members are generally not in the definition of any member method.
  • Outside the main program or class, object data members belong to instances (objects) and can only be accessed by object name;
  • Class data members belong to a class and are accessed by class name or object name.

  • Using the sharing of class data members, you can get the number of objects of this class in real time, and you can control the maximum number of objects created by this class

Insert picture description here

6.2.3 Member methods, class methods, static methods, abstract methods

Published 589 original articles · 300 praises · 80,000 + views

Guess you like

Origin blog.csdn.net/zhoutianzi12/article/details/105574938