21-Detailed introduction of object-oriented basics in python

07-Object Oriented\01-Object Oriented Basics.pdf

1. Define the class

class Washer():
    def wash(self):
        print('我会洗衣服')
        print(self)

2. Create an object

对象名 = 类名()

1. Practical example

class Washer():
    def wash(self):
        print('我会洗衣服')
haier = Washer()

print(haier)
haier.wash()

Output:

<__main__.Washer object at 0x000002076E0C1FD0>
我会洗⾐衣服

3. self-the object itself calling the function

self refers to the object that called the function.

class Washer():
    def wash(self):
        print('我会洗衣服')
        print(self)
haier = Washer()

print(haier)
haier.wash()

Output:

<__main__.Washer object at 0x000001C91DB51FD0>
我会洗衣服
<__main__.Washer object at 0x000001C91DB51FD0>

The results obtained by printing the object and self are the same, and both are the storage addresses in the memory of the current object.

4. Add and get object properties

1. Add object properties outside the class

1. Grammar

对象名.属性名 =

2. Code Examples

class Washer():
    def wash(self):
        print('我会洗衣服')
        print(self)
haier = Washer()

print(haier)
haier.wash()

haier.width = 100
haier.height = 200

print(f'haier 洗衣机的高度是{haier.height}')
print(f'haier 洗衣机的宽度是{haier.width}')

Output:

<__main__.Washer object at 0x000001DD4A8A2FD0>
我会洗衣服
<__main__.Washer object at 0x000001DD4A8A2FD0>
haier 洗衣机的高度是200
haier 洗衣机的宽度是100

2. Get properties inside the class

1. Grammar

self.属性名

2. Code Examples

# 定义类
class Washer():
    def print_info(self):
# 类里面获取实例属性
        print(f'haier1洗衣机的宽度是{self.width}')
        print(f'haier1洗衣机的高度是{self.height}')
# 创建对象
haier1 = Washer()
# 添加实例例属性
haier1.width = 500
haier1.height = 800
haier1.print_info()

5. Magic method

In Python, __xx__()functions are called magic methods, which refer to functions with special functions.

1. __init__()

1. __init__() 方法的作用:Initialize the object

# 定义类
class Washer():
    def __init__(self):
        self.width = 300
        self.height = 200

    def print_info(self):
        print(f'洗衣机的宽度是{self.width}, 高度是{self.height}')
# 创建对象
haier = Washer()
# 添加实例属性

haier.print_info()

Output:

洗衣机的宽度是300, 高度是200

2. __init__()Matters needing attention

  • __init__()Method, which is called by default when an object is created, and does not need to be called manually
  • __init__(self) The self parameter in does not need to be passed by the developer, the python interpreter will automatically pass the current object reference.

3. With parameters __init__()

Thinking: A class can create multiple objects. How to set different initialization properties for different objects?
Answer: Pass parameters.

1. Code example

class Washer():
    def __init__(self, width, height):
        self.width = width
        self.height = height
    def print_info(self):
        print(f'洗衣机的宽度是{self.width}')
        print(f'洗衣机的高度是{self.height}')

haier1 = Washer(10, 20)
haier1.print_info()

haier2 = Washer(30, 40)
haier2.print_info()

Output:

洗衣机的宽度是10
洗衣机的高度是20
洗衣机的宽度是30
洗衣机的高度是40

2. __str__()

When using print to output an object, the memory address of the object is printed by default. If the class is defined了__str__method, it will return the print data from this method.

1. Code example

class Washer():
    def __init__(self, width, height):
        self.width = width
        self.height = height
    def __str__(self):
        return '这是海尔洗衣机的说明书'
haier1 = Washer(10, 20)
# 这是海尔洗衣机的说明书
print(haier1)

Output:

这是海尔洗衣机的说明书

3. __del__()

When you delete an object, python explain器will call the default __del__()method.

1. Code example

class Washer():
    def __init__(self, width, height):
        self.width = width
        self.height = height
    def __del__(self):
        print(f'{self}对象已经被删除')
haier1 = Washer(10, 20)
# <__main__.Washer object at 0x0000026118223278>对象已经被删除
del haier1

Output:

<__main__.Washer object at 0x0000022F8FBC1FD0>对象已经被删除

Guess you like

Origin blog.csdn.net/sgy1993/article/details/114980722