定义一个雁类 Geese

class Geese: # 类属性 color = '白色' weight = '5kg' wing_span = '2m' instance_count = 0

# 构造方法
def __init__(self):
    # 每次实例化时,类属性instance_count加1
    Geese.instance_count += 1
    # 输出类属性
    print('颜色:', Geese.color)
    print('重量:', Geese.weight)
    print('翼展:', Geese.wing_span)
    print('编号:', Geese.instance_count)

实例化4只雁

for i in range(4): g = Geese()

Python 类定义:雁类 Geese 实例

原文地址: https://www.cveoy.top/t/topic/ohwU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录