python基础学习(十一)
程序员文章站
2023-01-22 17:34:26
22.类 # 类 class # 实例 实体 instance class Student: # 空语句 保持结构的完整性 pass jack = Student() jack.name = "Song Ke" print(jack.name) pony = Student() pony.name ......
22.类
# 类 class # 实例 实体 instance class student: # 空语句 保持结构的完整性 pass jack = student() jack.name = "song ke" print(jack.name) pony = student() pony.name = "pony" print(pony.name)
run结果:
23.构造函数
# 构造函数 constructor class student: # 声明构造函数 系统调用的 def __init__(self, name, age, sex): self.name = name self.age = age self.sex = sex print("__init__ is run") jack = student("jack.a", 21, "男") pony = student("pony.b", 22, "女") print(jack.name, jack.age, jack.sex) print(pony.name, pony.age, pony.sex)
run结果:
上一篇: Python基础(十五)