定义一个雁类Geese

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

# 定义构造方法
def __init__(self):
    # 将记录实例编号的类属性进行加1操作
    Geese.instance_count += 1
    # 输出4个类属性的值
    print('颜色:', Geese.color)
    print('重量:', Geese.weight)
    print('翼展:', Geese.wing_span)
    print('编号:', Geese.instance_count)

通过for循环创建4个雁类的实例

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

python定义一个雁类Geese并在该类中定义4个类属性前三个用于记录雁类的特征第4个用于记录实例编号然后定义一个构造方法在该构造方法中将记录实例编号的类属性进行加1操作并输出4个类属性的值最后通过for循环创建4个雁类的实例。简单一些中文版

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

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