程序员是要讲求生产力的,一天的练习,晚上再回顾一下.
类和对象*
1 2 3 4 5 6 7 8 9 10 11 12 13
   |  class Washer():          def wash(self):         print("我会洗衣服")         print(self)  
 
  haier1 = Washer()
  print(haier1)
  haier1.wash()
 
  | 
 
添加和获取对象的属性
1 2 3 4 5 6 7 8 9 10 11 12
   |  class Washer():          def print_info(self):         print(f"洗衣机的宽度是{self.width}")         print(f"洗衣机的高度是{self.height}")
 
  haier = Washer() haier.height=100 haier.width = 50 haier.print_info()
 
  | 
 
init 方法,初体验
1 2 3 4 5 6 7 8 9 10 11 12 13 14
   | class Washer():          def __init__(self):         self.width = 50         self.height = 100
      def print_info(self):                  print(f"洗衣机的高度是{self.height},洗衣机的宽度是{self.width}")
 
  haier = Washer() print(haier.height,haier.width) haier.print_info()
   | 
 
带参数的init方法(动态传递参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14
   | class Washer():          def __init__(self, width, height):         self.width = width         self.height = height          def print_info(self):                  print(f"洗衣机的高度是{self.height}宽度是{self.width}")
 
  haier = Washer(50, 100)  
  haier.print_info()
   | 
 
魔术方法
str()方法
当使用print输出对象的时候,默认打印对象的内存地址。
如果类定义了__str__方法,那么就会打印从在这个方法中 return 的数据。
1 2 3 4 5 6 7 8 9 10 11 12
   | class Washer():          def __init__(self, width, height):         self.width = width         self.height = height     def __str__(self):         return f"这是haier洗衣机的说明书: 高度{self.height} 宽度{self.width}"
 
  haier = Washer(50, 100)
  print(haier)
   | 
 
del()方法
1 2 3 4 5 6 7 8 9 10
   | class Washer():     def __init__(self, width, height):         self.width = width         self.height = height     def __del__(self):         print(f"{self}方法已经删除")
  haier = Washer(50, 100)
  del haier
   | 
 
案例1 烤地瓜
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
   | class SweetPotato():          def __init__(self):                  self.cook_time = 0                  self.cook_static = '生的'                  self.condiments = []
           def cook(self, time):         self.cook_time += time         if 0 <= self.cook_time < 3:             self.cook_static = '生的'         elif 3 <= self.cook_time < 5:             self.cook_static = '半生不熟'         elif 5 <= self.cook_time < 8:             self.cook_static = '熟了'         elif self.cook_time >= 8:             self.cook_static = '烤糊了'
           def __str__(self):         return f"这个地瓜烤了{self.cook_time}分钟,状态是{self.cook_static},添加的调料有{self.condiments}"
           def add_condiment(self,condiments1,condiments2):         self.condiments.append(condiments1)         self.condiments.append(condiments2)
 
  potato = SweetPotato()
  potato.cook(2) potato.add_condiment("酱油", "辣椒面") print(potato)
   | 
 
案例2 搬家具
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
   | class Furniture():          def __init__(self, name, area):                  self.name = name                  self.area = area
 
  class House():          def __init__(self, location, area):                           self.location = location                  self.area = area                  self.free_area = area                  self.furniture = []          def __str__(self):         return f"房子的地理位置为{self.location},占地面积为{self.area},剩余面积为{self.free_area},家具列表为{self.furniture}"
           def add_furniture(self, item):                  if self.free_area > item.area:             self.furniture.append(item.name)             self.free_area -= item.area         else:             print("家具太大,剩余面积不足,无法容纳!")
  bed = Furniture('双人床', 6) home = House('北京', 1200) print(home) home.add_furniture(bed) print(home)
   |