Fifteen of the introduction to python -- classes and objects

I. definition

  • Class is a general term for a series of things with the same characteristics and behaviors. It is an abstract concept, not a real thing.
  • Object is the real thing created by class.
  • In development, there are classes before objects. eg: student class, student a object
#Define washing machine category
"""
class Class name(): 
//Code
"""
class Washer():
def wash(self):  # self Refers to the object that calls the function.
print('Can wash clothes')

#create object
#Object name=Class name ()
heier=Washer()

print(heier)
heier.wash()

 

2, Magic method

In python, the function of u xx U () is called magic method, which refers to the function with special functions.

P.s: two underscores on both sides

1,__init__()

Initializing objects

class Washer():
    def __init__(self,high,width):
        self.high=high
        self.width=width
    def wath(self):

        print(f'Width of washing machine{self.width},')
        print(f'Height of washing machine{self.high}')

heier1 = Washer(10,20)
heier1.wath()"""
//The width of the washing machine is 20,
//Washing machine height 10
"""

2,__str__()

When you use print to output an object, the memory address of the object is printed by default.

If the class defines the \\\\\\\\\\.

class Washer():
    def __init__(self):
        self.high=500
    def __str__(self):
        return 'A description of a class or state of an object'

heier1 = Washer()
print(heier1)

"""
//A description of a class or state of an object
"""  

3,__del__()

Called when the object is deleted

class Washer():
    def __init__(self):
        self.high=500
    def __del__(self):
        print('Object deletion')

heier1 = Washer()

"""
//Object deletion
"""

3, Case realization

1. Roasted sweet potato

class SweetPotato():
    def __init__(self):
        self.time = 0
        self.static = 'Raw'
        self.condiments = []

    def cook(self, time):
        while True:
            self.time += 1
            if self.time>time:
                break
            if 0 <= self.time < 3:
                self.static = 'Raw'
            elif 3 <= self.time < 5:
                self.static = 'Halfcooked'
            elif 5 <= self.time < 8:
                self.static = 'Ripe'
            elif 8 <= self.time:
                self.static = 'Paste up'
            print(self)

    def __str__(self):
        return f'The sweet potato baked.{self.time}Minute,Status as{self.static}'

    def __del__(self):
        print('Deleted and deleted.')


digua1 = SweetPotato()
digua1.cook(10)

2. Moving furniture

class Furniture():
    def __init__(self, name, area):
        self.name = name
        self.area = area

class Home():
    def __init__(self, address, area):
        self.address = address
        self.area = area
        self.free_area = area
        self.furniture = []

    def __str__(self):
        return f'Location of the house{self.address},The housing area is{self.area},The remaining area of the house is{self.free_area},furniture{self.furniture}'

    def add_furniture(self, item):
        if item.area <= self.free_area:
            self.furniture.append(item.name)
            self.free_area -= item.area
        else:
            print('Furniture too large')

bed = Furniture('Double bed',6)
safa = Furniture('Sofa',6)
jia=Home('Beijing',1000)
print(jia)

jia.add_furniture(bed)
print(jia)

jia.add_furniture(safa)
print(jia)
"""
//The location of the house is Beijing. The area of the house is 1000. The remaining area of the house is 1000. The furniture []
//The location of the house is Beijing. The area of the house is 1000. The remaining area of the house is 994. The furniture ['double bed']
//The location of the house is Beijing. The area of the house is 1000. The remaining area of the house is 988. The furniture ['double bed', 'sofa']
"""

Keywords: Python

Added by robertsamanek on Mon, 30 Mar 2020 17:19:58 +0300