6.6 Python object oriented (6): class specific methods

@ overview

  • When a class does not inherit from any class, it inherits the object class of the system by default
  • In the object class, there are many special methods defined. Their method names are as follows: xxx, with two underscores on the left and right;
  • Proprietary methods are not used to call the instance directly, but have specific purposes. For example, init is called when the class name is called outside to create an instance;
  • For example, when the external print(obj) is used, the output string is actually from the return value of the obj corresponding class's ﹣ str ﹣ method;

@Common proprietary methods

  • ___init: constructor, called when the object is generated
  • ___del: destructor, used when releasing objects
  • ___str: plot style of the instance
  • ___Get len gth
  • ___gt: compare size (after arithmetic operation between objects, a calculated result should be returned)
  • ___add: addition operation
  • ___sub: subtraction
  • ___mul: multiplication
  • ___mod:
  • ___pow: Power

@Case: for a more standardized Person, the requirements are as follows:

  • Print log on object creation
  • Print log on object destruction
  • Custom object plot styles
  • Try to count people's "length"

The implementation code is as follows

# Encapsulate a Person class, and combine human related properties and methods for future reuse
class Person:
    # attribute
    name = "Lin Ai Hua"
    age = 20
    rmb = 50

    # Constructor: called when an instance of a class is created
    # Constructor is the best time to initialize instance properties
    def __init__(self, name, age, rmb):
        print("__init__:Labor was created")
        self.name = name
        self.age = age
        self.rmb = rmb

    # Destructor, called when an object is deleted
    def __del__(self):
        print("__del__:I was recycled")

    # Provides a string for printing when the object is printed
    def __str__(self):
        return "{name:%s;age:%d;rmb:%.2f}" % (self.name, self.age, self.rmb)

    # Returns the length of an object. How to calculate the length is user-defined
    def __len__(self):
        return int(self.rmb)

    # Compare whether the current instance is larger than an other instance of the same type, and customize the comparison method
    def __gt__(self, other):
        if self.rmb > other.rmb:
            return True
            return False

    # Define the result of adding to another instance
    def __add__(self, other):
        return Person(self.name + "-" + other.name, min(self.age, other.age), 
            self.rmb + other.rmb)

    # Define the result of subtracting from another instance
    def __sub__(self, other):
        return Person(self.name + "VS" + other.name, 
            self.age+other.age, abs(self.rmb - other.rmb))

    # Define the result of multiplying with another instance
    def __mul__(self, other):
        return self

    # Define the result of moduling another object
    def __mod__(self, other):
        return other

    # Self introduction method
    # self = instance of class
    def tell(self):
        print("I am%s,I%d I'm old. I have a deposit%.2f Ten thousand yuan" % (self.name, self.age, self.rmb))

Create objects and perform printing, comparison, operation, etc

    # Create an instance of the Person class
    p1 = Person("Yi Tian", 60, 500)
    p2 = Person("Lin Ai Hua", 20, 10)

    # Call instance method

    # Call the str method to print the object

    # Call the method to find the object len gth

    # Call the method to compare
    print(p1 > p2)

    # Call the add method to add
    print(p1 + p2)

    # Call the sub method to add
    print(p1 - p2)

    # Call the method to add
    print(p1 * p2)

    # Call the add method
    print(p1 % p2)

    # At the end of the program, the object is destroyed by the system, and the del method of the object is called

results of enforcement

Tags: Attribute

Posted on Sat, 04 Apr 2020 03:33:18 -0700 by hussainz2000