# coding:utf-8
stu_obj = {
'stu_name': 'SEPIA',
'stu_age': 21,
'stu_gender': 'male'
}
class Student:
# 变量定义
stu_school = "oldboy"
count = 0
# 类在调用阶段会自动触发__init__
# 调用时自动将stu1_obj传给第一个参数
def __init__(self, x, y, z):
Student.count += 1
self.stu_name = x
self.stu_age = y
self.stu_gender = z
# 功能定义
def tel_stu_info(self):
print('学生信息:名字: %s 年龄: %s 性别: %s' % (
self.stu_name,
self.stu_age,
self.stu_gender
))
def set_info(self, x, y, z):
self.stu_name = x
self.stu_age = y
self.stu_gender = z
print('!!!!!!!!!!!!!!!!!!!')
# 类中存放的是对象共有的数据和功能
# 一、类可以访问:
# 1.类的数据属性
print(Student.stu_school)
# 2.类的函数属性
print(Student.tel_stu_info)
print(Student.set_info)
# 二、但其实类中的东西是给对象用的
# 1.类的数据属性是共享给所有对象用的,大家访问的地址都一样
stu1_obj = Student('aaa', 22, 'male')
print(Student.count)
stu2_obj = Student('aaa', 22, 'male')
print(Student.count)
# 2.类的函数属性是绑定给对象用的,虽然所有对象指向的都是相同的功能,但是绑定到不同的对象就是不同的绑定方法,内存地址也不同
# 绑定方法的特殊之处在于,谁来调用绑定方法,就会将谁当做第一个参数自动传入
# stu1_obj = Student('aaa', 22, 'male')
# print(stu1_obj.__dict__)
# print(stu1_obj.stu_name)
# print(stu1_obj.stu_age)
# print(stu1_obj.stu_gender)
# print(stu1_obj.stu_school)
# print(Student.count)
# stu2_obj = Student('aaa', 22, 'male')
# print(Student.count)
stu1_obj.tel_stu_info()