欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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结果:

python基础学习(十一)

 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基础学习(十一)