python基础学习(十一)

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

 

原文地址:https://www.cnblogs.com/songxiaoke/p/11890425.html