Python object-oriented programming and the concept of objects

First of all, it needs to be clear that object-oriented programming is not unique to python;
object-oriented is a programming idea;
in object-oriented thinking

everything is an object

Object-oriented simple understanding:

Object-oriented is to compose a set of data structures and methods for processing them into objects, classify objects with the same behavior into classes, hide the internal details of classes by encapsulation, generalize classes through inheritance, and realize object-based object type-based Dynamic classification.

what is class

A class represents a group (or class) of objects, and each object belongs to a particular class and is called an instance of that class.
In object-oriented programming, you write classes that represent things and situations in the real world, and create objects based on those classes.
When you write a class, you define behavior that is common to a large class of objects. When you create objects based on classes, each object automatically has this common behavior, and you can then give each object a unique personality as needed. Creating an object from a class is called instantiation, which allows you to use an instance of the class
In object-oriented programming, the term object roughly means a collection of data (properties) and a set of methods to access and manipulate these data; and method composition. Properties are nothing but variables that belong to an object, while methods are functions stored in properties.

  • The creation of classes in python

class Person():
    
    def __init__(self):
        pass

    def set_name(self, name):
        self.name = name

    def get_name(self):
        return self.name
    
    def greet(self):
        print("Hello, world! I'm {}.".format(self.name))

Person is the name of the class that contains three method definitions, and the class statement creates a separate namespace in which to define functions. The parameter self points to the object itself.
The method init () is a special method that Python runs automatically whenever a new instance is created. In the name of this method, there are two underscores at the beginning and two at the end, which is a convention designed to avoid name conflicts between Python default methods and ordinary methods.
self is useful, even essential. Without it, none of the methods have access to the object itself—the object whose property to manipulate belongs to;
by default, an object's properties are externally accessible. To make a property inaccessible from outside the object, define the property as private. Private and only accessible through accessor methods
Python does not provide direct support for private properties, to make a method or property private (not accessible from the outside), just make its name start with two underscores

what is the object

An object is an entity in the real world, and there is a one-to-one correspondence between an object and an entity, that is to say, every entity in the real world is an object, which is a specific concept. Objects have the following characteristics:
Objects have properties and behaviors.
Objects have a changing state.
Objects are unique.

Objects are instances of a class.

An object is an instance of a data structure defined by a class

person = Person()

Guess you like

Origin blog.csdn.net/weixin_42551921/article/details/124447593