属性查找
类有两种属性:数据属性和函数属性
1、类的数据属性是所有对象共享的
class LuffyStudent: school=‘luffycity‘ def learn(self): print(‘is learning‘) def eat(self): print(‘is sleeping‘) #后产生对象 stu1=LuffyStudent() stu2=LuffyStudent() stu3=LuffyStudent() print(id(stu1.school)) print(id(stu2.school)) print(id(stu3.school)) # 43496496 # 43496496 # 43496496
2、类的函数数据是绑定给对象用的,称为绑定到对象的方法
#后产生对象 stu1=LuffyStudent() stu2=LuffyStudent() stu3=LuffyStudent() #类的函数属性是绑定给对象使用的,obj.method称为绑定方法,内存地址都不一样 print(stu1.learn()) print(stu2.learn()) print(stu3.learn()) # <bound method LuffyStudent.learn of <__main__.LuffyStudent object at 0x00000000127085C0>> # <bound method LuffyStudent.learn of <__main__.LuffyStudent object at 0x0000000012708518>> # <bound method LuffyStudent.learn of <__main__.LuffyStudent object at 0x0000000012708630>> #ps:id是python的实现机制,并不能真实反映内存地址,如果有内存地址,还是以内存地址为准
在obj.name会先从obj自己的名称空间里找name,找不到则去类中找,类也找不到就找父类...最后都找不到就抛出异常
绑定方法
定义类并实例化出三个对象
class LuffyStudent: school=‘luffy‘ def __init__(self,name,age): self.name = name self.age = age def eat(self): print(‘%s is eating‘ %self.name) def sleep(self): print(‘%s is sleeping‘ %self.name) def __del__(self): print("running del method, this person must be died.") # 后产生对象 stu1=LuffyStudent(‘alex‘,18) stu2=LuffyStudent(‘li‘,28) stu3=LuffyStudent(‘hy‘,38) stu1.eat() stu2.eat() stu3.eat() # alex is eating # li is eating # hy is eating
强调:绑定到对象的方法的特殊之处在于,绑定给谁就由谁来调用,谁来调用,就会将‘谁’本身当做第一个参数传给方法,即自动传值(方法__init__也是一样的道理)
stu1.eat() # 等同于LuffyStudent.eat(stu1) stu2.eat() # 等同于LuffyStudent.eat(stu1) stu3.eat() # 等同于LuffyStudent.eat(stu1)
注意:绑定到对象的方法的这种自动传值的特征,决定了在类中定义的函数都要默认写一个参数self,self可以是任意名字,但是约定俗成地写出self。
类即类型
python中一切皆为对象,且python3中类与类型是一个概念,类型就是类
#类型dict就是类dict >>> list <class ‘list‘> #实例化的到3个对象l1,l2,l3 >>> l1=list() >>> l2=list() >>> l3=list() #三个对象都有绑定方法append,是相同的功能,但内存地址不同 >>> l1.append <built-in method append of list object at 0x10b482b48> >>> l2.append <built-in method append of list object at 0x10b482b88> >>> l3.append <built-in method append of list object at 0x10b482bc8> #操作绑定方法l1.append(3),就是在往l1添加3,绝对不会将3添加到l2或l3 >>> l1.append(3) >>> l1 [3] >>> l2 [] >>> l3 [] #调用类list.append(l3,111)等同于l3.append(111) >>> list.append(l3,111) #l3.append(111) >>> l3 [111]
数据与功能分离
class MySQLHandler: def __init__(self,host,port,db,charset=‘utf8‘): self.host=host self.port=port self.db=db self.charset=charset self.conn=self.connect(self.host,self.port,self.db,self.charset) def exc1(self,sql): return self.conn.execute(sql) def exc2(self,sql): return self.conn.call_proc(sql) def _connect(self,host,port,db,charset): print(‘connect %s,%s,%s,%s‘ %(host,port,db,charset)) obj=MySQLHandler(‘127.0.0.1‘,3306,‘db1‘) obj.exc1(‘select * from tb1;‘) obj.exc2(‘mycall‘)
小节练习
练习1:编写一个学生类,产生一堆学生对象
要求:
- 有一个计数器(属性),统计总共实例了多少个对象
class Student(object): count = 0 def __init__(self,name): self.name = name Student.count+=1 def learn(self): print(‘%s is learning‘%self.name) s1 = Student(‘x1‘) print(s1.count) # 1 s2 = Student(‘x2‘) print(s2.count) # 2 s3 = Student(‘x3‘) print(s3.count) # 3
练习2:模仿王者荣耀定义两个英雄类
要求:
- 英雄需要有昵称、攻击力、生命值等属性;
- 实例化出两个英雄对象;
- 英雄之间可以互殴,被殴打的一方掉血,血量小于0则判定为死亡。
class HeroA: camp = ‘A‘ def __init__(self,name,life_vale,aggresivity): self.name = name self.life_value = life_vale self.aggresivity = aggresivity def attack(self,enemy): if enemy.life_value == 0: print(‘%s is dead‘ %enemy.name) enemy.life_value -= self.aggresivity class HeroB: camp =‘B‘ def __init__(self,name,life_vale,aggresivity): self.name = name self.life_value = life_vale self.aggresivity = aggresivity def attack(self,enemy): if enemy.life_value == 0: print(‘%s is dead‘ % enemy.name) enemy.life_value -= self.aggresivity a = HeroA(‘a1‘,160,50) b = HeroB(‘b1‘,150,40) while b.life_value > 0: a.attack(b) print(‘b life‘, b.life_value) b.attack(a) print(‘a life‘, a.life_value) a.attack(b) #结果 b life 100 a life 120 b life 50 a life 80 b life 0 a life 40 b1 is dead
原文地址:https://www.cnblogs.com/xiao-apple36/p/9127751.html
时间: 2024-11-05 18:45:27