python-类
# coding=utf-8
'''
class 类名:
属性
特征
'''
class Cat: cat_color='白色' cat_foot=4 cat_weight=20 def eat(self): #self 代表创建的对象,能够区分调用的对象是谁 print(f'{self.name}小猫吃鱼') def catch(self): print('小猫捉老鼠') # 实例化对象 tom = Cat() tom.name='tom' tom.eat() tom.catch()
tom小猫吃鱼
小猫捉老鼠
# __init__初始化函数只要创建类对象的时候,就会执行这个函数
class Cat: def __init__(self,color,foot,name): self.color=color self.foot=foot self.name=name def eat(self): print(f'{self.name},{self.color}小猫哎吃鱼') def catch(self): print('猫捉老鼠') tom = Cat('黑色',4,'五条') # tom.name='tom' tom.eat()
五条,黑色小猫哎吃鱼
# del 对象被销毁前会调用的函数
class Cat: def __init__(self,color,foot,name): self.color=color self.foot=foot self.name=name def eat(self): print(f'{self.name},{self.color}小猫哎吃鱼') def catch(self): print('猫捉老鼠') def __del__(self): print(f'{self.name}小猫走了') c=Cat('白色',4,'guai') print('3')
3
guai小猫走了