Day27.属性查找与绑定方法
1.属性查找与绑定方法_类和类下的对象访问数据属性
类和类下的对象访问数据属性代码如下:
class Student: # 1.变量的定义 stu_school = 'oldboy' # 记录类下实例化次数 count = 0 # 空对象,'egon', 18, 'male' def __init__(self, x, y, z): # 每次实例化进行加1操作 Student.count += 1 self.stu_name = x # 空对象.stu_name='egon' self.stu_age = y # 空对象.stu_age=18 self.stu_gender = z # 空对象.stu_gender='male' return None # 2.功能的定义 # 学生的功能 def tell_stu_info(self): print('学生信息:名字:{} 年龄:{} 性别:{}'.format( 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 def choose(self, x): print('正在选课...') self.course = x stu1_obj = Student('egon', 18, 'male') # 原理:Student.__init__(空对象,) stu2_obj = Student('lili', 19, 'female') stu3_obj = Student('jack', 20, 'male') # 找属性,类中存放的是对象共有的数据与功能 ''' 一:类可以访问:''' print('类可以访问:1.类的数据属性'.center(40, '=')) print(Student.stu_school) print('类可以访问:2.类的函数属性'.center(40, '=')) print(Student.tell_stu_info) print(Student.set_info) # 二:类中的东西是给对象用的 # 1.类的数据属性是共享给所有对象用的,大家访问的地址一样 print('类的数据属性是共享给所有对象用的,访问的地址一样'.center(40, '=')) print(Student.stu_school, id(Student.stu_school)) print(stu1_obj.stu_school, id(stu1_obj.stu_school)) print(stu2_obj.stu_school, id(stu2_obj.stu_school)) print(stu3_obj.stu_school, id(stu3_obj.stu_school))
2.属性查找与绑定方法_重定义类下的数据内容和记录下实例化次数
重定义类下的数据内容和记录下实例化次数,代码如下:
class Student: # 1.变量的定义 stu_school = 'oldboy' # 记录类下实例化次数 count = 0 # 空对象,'egon', 18, 'male' def __init__(self, x, y, z): # 每次实例化进行加1操作 Student.count += 1 self.stu_name = x # 空对象.stu_name='egon' self.stu_age = y # 空对象.stu_age=18 self.stu_gender = z # 空对象.stu_gender='male' return None # 2.功能的定义 # 学生的功能 def tell_stu_info(self): print('学生信息:名字:{} 年龄:{} 性别:{}'.format( 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 def choose(self, x): print('正在选课...') self.course = x stu1_obj = Student('egon', 18, 'male') # 原理:Student.__init__(空对象,) stu2_obj = Student('lili', 19, 'female') stu3_obj = Student('jack', 20, 'male') print('原数据:oldboy,对象重定义指定对象下的数据内容,不会影响其他函数下的数据内容'.center(60, '=')) stu1_obj.stu_school = 'OLDBOY' print(stu1_obj.stu_school) print(Student.stu_school) print(stu2_obj.stu_school) print(stu3_obj.stu_school) print('类重定义类下的数据内容'.center(40, '=')) Student.stu_school = 'OLDBOY' print(Student.stu_school) print('class.count方法,记录实例化次数'.center(40, '=')) print(Student.count)
3.属性查找与绑定方法_类中函数属性绑定给对象使用
类中函数属性绑定给对象使用,代码如下:
class Student: # 1.变量的定义 stu_school = 'oldboy' # 记录类下实例化次数 count = 0 # 空对象,'egon', 18, 'male' def __init__(self, x, y, z): # 每次实例化进行加1操作 Student.count += 1 self.stu_name = x # 空对象.stu_name='egon' self.stu_age = y # 空对象.stu_age=18 self.stu_gender = z # 空对象.stu_gender='male' return None # 2.功能的定义 # 学生的功能 def tell_stu_info(self): print('学生信息:名字:{} 年龄:{} 性别:{}'.format( 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 def choose(self, x): print('正在选课...') self.course = x stu1_obj = Student('egon', 18, 'male') # 原理:Student.__init__(空对象,) stu2_obj = Student('lili', 19, 'female') stu3_obj = Student('jack', 20, 'male') ''' 二.类的函数属性是绑定给对象用的, 而且是绑定给对象的,虽然所有对象指向的都是相同的功能, 但是绑定到不同的对象就是不同的绑定方法,内存地址各不相同 ''' # print(Student.tell_stu_info) # print(Student.set_info) # 类调用紫的函数必须严格按照函数的用法来 print('类中函数属性绑定给对象用'.center(50, '=')) Student.tell_stu_info(stu1_obj) Student.tell_stu_info(stu2_obj) Student.tell_stu_info(stu3_obj)
4.属性查找与绑定方法_修改类中函数信息和访问类下函数的属性
修改类中函数信息和访问类下函数的属性,代码如下:
class Student: # 1.变量的定义 stu_school = 'oldboy' # 记录类下实例化次数 count = 0 # 空对象,'egon', 18, 'male' def __init__(self, x, y, z): # 每次实例化进行加1操作 Student.count += 1 self.stu_name = x # 空对象.stu_name='egon' self.stu_age = y # 空对象.stu_age=18 self.stu_gender = z # 空对象.stu_gender='male' return None # 2.功能的定义 # 学生的功能 def tell_stu_info(self): print('学生信息:名字:{} 年龄:{} 性别:{}'.format( 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 def choose(self, x): print('正在选课...') self.course = x stu1_obj = Student('egon', 18, 'male') # 原理:Student.__init__(空对象,) stu2_obj = Student('lili', 19, 'female') stu3_obj = Student('jack', 20, 'male') print('修改类中指定函数的属性信息'.center(50, '=')) Student.set_info(stu1_obj, 'EGON', 18, 'MALE') Student.tell_stu_info(stu1_obj) # 绑定方法的特殊之处在于:谁来调用绑定方法就会将谁当做第一个参数自动传入 print('类访问自己的函数属性'.center(50, '=')) print(Student.tell_stu_info) print(stu1_obj.tell_stu_info) print(stu2_obj.tell_stu_info) print(stu3_obj.tell_stu_info)
5.属性查找与绑定方法_访问自己的函数属性,调用类下的函数方法
访问自己的函数属性,调用类下的函数方法:
class Student: # 1.变量的定义 stu_school = 'oldboy' # 记录类下实例化次数 count = 0 # 空对象,'egon', 18, 'male' def __init__(self, x, y, z): # 每次实例化进行加1操作 Student.count += 1 self.stu_name = x # 空对象.stu_name='egon' self.stu_age = y # 空对象.stu_age=18 self.stu_gender = z # 空对象.stu_gender='male' return None # 2.功能的定义 # 学生的功能 def tell_stu_info(self): print('学生信息:名字:{} 年龄:{} 性别:{}'.format( 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 def choose(self, x): print('正在选课...') self.course = x stu1_obj = Student('egon', 18, 'male') # 原理:Student.__init__(空对象,) stu2_obj = Student('lili', 19, 'female') stu3_obj = Student('jack', 20, 'male') stu1_obj.tell_stu_info() # 等同于 tell_stu_info(stu1_obj) stu2_obj.tell_stu_info() # 等同于 tell_stu_info(stu2_obj) stu3_obj.tell_stu_info() # 等同于 tell_stu_info(stu3_obj)
6.属性查找与绑定方法_类中选课方法函数的功能实例
类中选课方法函数的功能实例,代码如下:
class Student: # 1.变量的定义 stu_school = 'oldboy' # 记录类下实例化次数 count = 0 # 空对象,'egon', 18, 'male' def __init__(self, x, y, z): # 每次实例化进行加1操作 Student.count += 1 self.stu_name = x # 空对象.stu_name='egon' self.stu_age = y # 空对象.stu_age=18 self.stu_gender = z # 空对象.stu_gender='male' return None # 2.功能的定义 # 学生的功能 def tell_stu_info(self): print('学生信息:名字:{} 年龄:{} 性别:{}'.format( 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 def choose(self, x): print('正在选课...') self.course = x stu1_obj = Student('egon', 18, 'male') # 原理:Student.__init__(空对象,) stu2_obj = Student('lili', 19, 'female') stu3_obj = Student('jack', 20, 'male') print('三.选课功能实例'.center(40, '=')) stu1_obj.choose('学生1选择的课程:python全栈开发') print(stu1_obj.course) stu2_obj.choose('学生2选择的课程:Linux运维') print(stu2_obj.course) stu3_obj.choose('学生3选择的课程:高级架构师') print(stu3_obj.course)
7.属性查找与绑定方法_列表内置函数方法讲解
列表内置函数方法讲解,代码如下:
'''四.列表内置函数方法使用''' l1 = [1, 2, 3] # l = list([1, 2, 3]) l2 = [111, 222, 333] list.append(l1, 4) list.append(l2, 4444) print('列表内置函数方法使用'.center(40, '=')) print('l1:{}'.format(l1)) print('l2:{}'.format(l2))