This is the 19th day of my participation in the Genwen Challenge

Review and

Last time we had a basic understanding of object-oriented programming.

When we analyze things, we keep taking them apart until we find their essence.

Similarly, object oriented thought is also like this, a complex task, to classify and disassemble different modules, and then use process oriented to realize the specific functions of each module.

Through object-oriented thinking, we have the concept of classes and objects.

A class is a template for an object, and an object is a concrete object of a class

In this installment, we’ll look at physical objects of classes, also known as instances.

1. Instance properties

1.1 Introduction to Instance Properties

Instance properties are properties that are subordinate to the instance object, also known as instance variables.

  • Instance attributes are typically placed in the __init__() method

    Self. instance attribute name = initial valueCopy the code
  • In other instance methods of the class, access is made through self

    Self. instance attribute nameCopy the code
  • Once the instance object is created, it is accessed through the instance object

    Obj1. Instance attribute name = value # Attribute assigned to the new objectCopy the code

1.2 Memory analysis of instance attributes

We’re still taking Animal as an example.

Class Animal: def __init__(self,name,food): # Animal def __init__(self,name,food) Animal("Tom","fish") cat. Voice = "miaomiao" # Create cat new attribute voice cat.where = "China" # create cat new attribute where cat.like_food()Copy the code
  1. The first thing I do is create the class object Animal in the heap memory

  1. Animal(“Tom”,”fish”) calls the class constructor to create a property object in the heap and assign it to the stack variable cat

  1. Add new attributes voice and WHERE to the cat instance and add them to the property object via cat

Summary: Instance attributes are subordinate to instance objects. New attributes added to created objects do not affect other object attributes.

2. Instance method

2.1 Example method introduction

Instance methods are methods that depend on the instance object and are called only on the object

Instance methods are defined in the scope of the class in the following format:

Def method name (self,[parameter list]): function bodyCopy the code

Method is called in the following format:

Object. Method name (argument list)Copy the code

Note:

  1. When defining an instance method, the first argument must be self, which refers to the current object
  2. When an instance method is called, self cannot be passed as an argument; self is passed automatically by the interpreter

The difference between functions and instance methods

  1. Both are blocks of statements used to complete a function, essentially the same
  2. When a method is called, it is called through an object, and the method is subordinate to a specific instance object. Normal functions do not have this feature
  3. The method definition requires passing self; the function does not
2. Instance method memory analysis

And then we just learned about instance properties, so let’s go to the Animal class and learn about instance method memory operations

Class Animal: def __init__(self,name,food): # Animal def __init__(self,name,food) Dog = Animal("Bob","meat") dog = Animal("Bob","meat") dog.like_food(dog)Copy the code
  1. Create the Animal class in the heap with a lie_food method inside
  2. Animal(“Bob”,”meat”) creates a dog object that contains the dog property and the like_food method

To call the like_food method in the class, you need to pass in self (self is the address of the object). So dog.like_food() works just as well as animals.likefood (dog)

conclusion

This time as long as the learning class instance object, instance properties and instance method of the underlying logic has a clear understanding and mastery.

Next, we will learn the underlying principles related to object-oriented classes

Ok, that’s the content of this issue, welcome to comment section, see you next time ~