SoFunction
Updated on 2024-10-30

Interaction between python objects explained in detail

Let's first look at the general class definition as follows:

class class name:
  def __init__(self,parameters1,parameters2):
    self.Properties of an object1 = parameters1
    self.Properties of an object2 = parameters2

  def method name(self):pass

  def method name2(self):pass

object name = class name(1,2) #Objects are instances, representing a concrete thing
         #className() : class name + brackets is instantiating a class, which is equivalent to calling the __init__ method
         # Pass parameters in parentheses, parameters do not need to pass self, the others correspond to the formal parameter in init.
         # The result returns an object
object name.Properties of an object1  #To view the properties of an object, use the object name. Property Name
object name.method name()   #Calling methods in a class,direct object name.method name() can immediately (do sth)

Interaction between objects

Now that we have a human, by giving the human some specific attributes we get an actual human.
Now we're going to create another dog class, and the dog won't be able to hit people, it will only be able to bite them, so we'll give the dog a bite method.
With the dog class, we also have to instantiate an actual dog out of it.
Then man and dog can fight. Now let's make them fight!

class Person: # Define a human being
  role = 'person' # All human characters are human

  def __init__(self, name, aggressivity, life_value):
     = name # Each character has its own nickname; #
     = aggressivity # Each character has their own attack power; #
    self.life_value = life_value # Each character has their own life value; #

  def attack(self,dog):
    # A man can attack a dog, and here the dog is an object. #
    # A man attacks a dog, then the dog's life value drops according to the man's attack #
    dog.life_value -= 

class Dog: # Define a dog class
  role = 'dog' # Dogs with all the character attributes

  def __init__(self, name, breed, aggressivity, life_value):
     = name # Every dog has its own nickname #
     = breed # Every dog has its own breed #
     = aggressivity # Every dog has its own attack; #
    self.life_value = life_value # Every dog has its own life value; #

  def bite(self,people):
    # Dogs can bite, and here's an object. #
    # If a dog bites a person, then the person's life value drops according to the dog's aggression #
    people.life_value -= 

egg = Person('egon',10,1000) # Creates a real person eggprint() # In will first look for name from obj's own namespace, if it can't find it then go to the class, if the class can't find it then look for the parent class... If it doesn't find it, it throws an exception.
ha2 = Dog('Erdnase','Husky',10,1000) #♪ Created a real dog ha2
print(ha2.life_value)     #Look at ha2's life value #
(ha2)        #egg hit ha2 a little bit
print(ha2.life_value)     #ha2That's a good idea. That's a good idea.10faeces

Object-Oriented Combinatorial Usage

Combination refers to the use of an object of another class as a data attribute in one class and is called a combination of classes

class Weapon:
  def prick(self, obj): # It's the active skill of this equipment, stabbing the opponent #
    obj.life_value -= 500 # Assuming an attack of 500

class Person: # Define a human being
  role = 'person' # All human characters are human

  def __init__(self, name):
     = name # Each character has its own nickname; #
     = Weapon() # Bind a weapon to a character.
    
egg = Person('egon')
() 
#eggCombined a weapon object,You can use all the methods of the combined class directly.

A circle is made up of two circles, and the area of the circle is the area of the outer circle minus the area of the inner circle. The circumference of the ring is the circumference of the inner circle plus the circumference of the outer circle.
At this point, we will first implement a circle class to calculate the circumference and area of a circle. Then we combine the instances of the circle in the "Circle Class" as our own attribute to use

from math import pi

class Circle:
  '''
  Defines a circle class;
  Provides methods for calculating area and perimeter.
  '''
  def __init__(self,radius):
     = radius

  def area(self):
     return pi *  * 

  def perimeter(self):
    return 2 * pi *


circle = Circle(10) # Instantiate a circle
area1 = () #Calculate the area of a circle
per1 = () #Calculate the circumference of a circle
print(area1,per1) #Print the area and circumference of a circle

class Ring:
  '''
  Defines a circle class
  Provides methods for the area and perimeter of a circle
  '''
  def __init__(self,radius_outside,radius_inside):
    self.outsid_circle = Circle(radius_outside)
    self.inside_circle = Circle(radius_inside)

  def area(self):
    return self.outsid_circle.area() - self.inside_circle.area()

  def perimeter(self):
    return self.outsid_circle.perimeter() + self.inside_circle.perimeter()


ring = Ring(10,5) # Instantiate a ring
print(()) # Calculate the circumference of the ring
print(()) #Calculate the area of the ring

The relationship between the class and the class of the combination is established in a combinatorial way, it is a 'have' relationship, e.g. the professor has a birthday, the professor teaches a python course

class BirthDate:
  def __init__(self,year,month,day):
    =year
    =month
    =day

class Couse:
  def __init__(self,name,price,period):
    =name
    =price
    =period

class Teacher:
  def __init__(self,name,gender,birth,course):
    =name 
    =gender
    =birth
    =course
  def teach(self): 
    print('teaching')

p1=Teacher('egon','male', 
      BirthDate('1995','1','27'), 
      Couse('python','28000','4 months')
      ) 

print(,,) 

print(,,)
'''
Running results.
1995 1 27
python 28000 4 months
'''

The above is a detailed explanation of the interaction between python objects in detail, more information about python object interaction please pay attention to my other related articles!