#在类定义阶段就会立刻执行类体代码,会产生类的名称空间,用于将类体代码执行过程中产生的名字都存放与类的名称空间中
class OldboyStudent:
school = 'Oldboy'
def choose_course(self):
print('is choosing course')
# print('====>')
#类有两种用途
#用途一:类本身就一个容器(名称空间),所以可以增删改查类的属性
# print(OldboyStudent.__dict__)
# print(OldboyStudent.__dict__['school'])
# print(OldboyStudent.__dict__['choose_course'])
# OldboyStudent.__dict__['choose_course'](123)
# print(OldboyStudent.school) #OldboyStudent.__dict__['school']
# print(OldboyStudent.xxx) #OldboyStudent.__dict__['xxx']
# print(OldboyStudent.choose_course)
# OldboyStudent.choose_course(123)
# OldboyStudent.country='China'
# OldboyStudent.school='Oldgirl'
# print(OldboyStudent.__dict__)
# del OldboyStudent.school
#
# print(OldboyStudent.__dict__)
#用途二:调用类来产生对象,调用类的过程又称之为实例化
x=1
class OldboyStudent:
school = 'Oldboy'
def choose_course(self):
print('is choosing course')
stu1=OldboyStudent() # stu1可称为一个对象,也可以称为一个实例
stu2=OldboyStudent() # stu1可称为一个对象,也可以称为一个实例
stu3=OldboyStudent() # stu1可称为一个对象,也可以称为一个实例
def init(obj,x,y,z):
obj.name=x
obj.sex=y
obj.age=z
#对象1 独有的特征
# name = '李泰迪'
# sex = 'male'
# age = 18
# stu1.name='李泰迪'
# stu1.sex='male'
# stu1.age=18
init(stu1,'李泰迪','male',18)
#对象2 独有的特征
# name = '牛榴弹'
# sex = 'female'
# age = 38
# stu2.name='牛榴弹'
# stu2.sex='female'
# stu2.age=38
init(stu2,'牛榴弹','female',38)
#对象3 独有的特征
# name = '张随便'
# sex = 'male'
# age = 38
# stu3.name='张随便'
# stu3.sex='male'
# stu3.age=38
init(stu3,'张随便','male',38)
# print(stu1.__dict__)
# print(stu2.__dict__)
# print(stu3.__dict__)
# !!!!对象的属性查找是先从对象自己的名称空间中找,找不到则取类中查找。。。。,类中存放的数据是所有对象共有的,内存地址都一样
# print(stu1.x)
# print(stu1.school,id(stu1.school))
# print(stu2.school,id(stu2.school))
# print(stu3.school,id(stu3.school))
print(stu1.choose_course,id(stu1.choose_course))
print(stu2.choose_course,id(stu2.choose_course))
print(stu3.choose_course,id(stu3.choose_course))