类
定义对象的属性
class CuteCat:
#格式:class + 类名 :
def __init__(self,cat_name,cat_age,cat_color):
#格式:def __init__(self, 参数名1, 参数名2, 参数名3):
self.name = cat_name
self.age = cat_age
self.color = cat_color
#格式:self.新变量名01 = 参数名1
#如果是固定参数:(例子)
# def __init__(self)
#self.qwq = 100
#创建对象
cat1 = CuteCat("jojo",2,"橙色")
print(f"小猫{cat1.name}的年龄是{cat1.age}岁,花色是{cat1.color}")
定义函数的方法
class CuteCat:
def speak(self):
#格式:def + 函数名(self):
print("喵" * 3)
#函数体
cat1 = CuteCat()
cat1.speak()