python day6 面向对象
Posted 一个人的北京
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python day6 面向对象相关的知识,希望对你有一定的参考价值。
把一个类变成具体的实例化的过程叫做类
实例化:把一个类变成一个具体对象的过程
__init__ 构造函数,在实例化时初始化数据
__init__方法的第一个参数永远是self,表示创建的实例本身,因此,在__init__方法内部,就可以把各种属性绑定到self,因为self就指向创建的实例本身。
class dog: def __init__(self): pass def res(self,name): print("%s miao xingren" % name) def bulk(self,age): print("%s miao xingren" %age) a1=dog() a1.bulk("dddd") a1.res("dddd")
__del__方法是个析构函数,是用来将实例销毁的函数,做收尾工作,例如关闭数据库等
# #----先去实例里去找,如果没有 再去类里去找 # class Role(object): # n=123 # name="alex" # n_list=[] # def __init__(self, role, weapon, life_value=100, money=15000): # #self.name = name # # self.role = role # # self.weapon = weapon # # self.__life_value = life_value # # self.money = money # # def shot(self): # print("shooting...") # # def got_shot(self): # print (type(self.__life_value)) # self.__life_value -=50 # print("ah...,I got shot...%s"%self.__life_value) # # def show_status(self): # return ("life_val:%s"%self.__life_value) # # def buy_gun(self, gun_name): # print("%s has just bought %s"%(self.name, gun_name) ) # def __del__(self): #析构函数 # pass # # print("%s 死了"%(self.name)) # r1 = Role(‘police‘, ‘AK47‘) #实例化 # r2 = Role( ‘terrorist‘, ‘B22‘) #生成一个角色 # r1.buy_gun("AKR1") # r1.got_shot() # print(r1.show_status()) #del r1
#子类继承父类的功能 class people: def __init__(self,name,age): self._NAME=name self._AGE=age def cry(self): print ("%s man "%self._NAME) def sleep(self): print("%s is sleep"%self._NAME) class man(people): def piao(self): print ("%s is piao..."%self._NAME) people.sleep(self) print("%s is sleep"%self._NAME) class women(people): def shop(self): print("%s is shop..."%self._NAME) M1=man("zhangsan",22) M1.piao() M1.sleep() M2=women("lisi",11) M2.shop()
# super用法 class people: def __init__(self,name,age): self._NAME=name self._AGE=age def cry(self): print ("%s man "%self._NAME) def sleep(self): print("%s is sleep"%self._NAME) class man(people): def __init__(self,name,age,money): #people.__init__(self,name,age) super(man,self).__init__(name,age) ###一般都用这个 self.MOENEY=money print("%s is money%s"%(self._NAME,self.MOENEY)) def piao(self): print ("%s is piao..."%self._NAME) people.sleep(self) print("%s is sleep"%self._NAME) class women(people): def shop(self): print("%s is shop..."%self._NAME) M1=man("zhangsan",22,100) M1.piao() M1.sleep() M2=women("lisi",11) M2.shop()
多态:一种接口 多种形态,多态是接口重用
#-*- coding:utf8 -*- class school(object): def __init__(self,name,adree): self._name=name self._addree=adree self.students=[] self.teachers=[] self.staffs=[] def enroll(self,stu_obj): print("为%s同学办理入学手续"%stu_obj._name) self.students.append(stu_obj) def hire(self,staff_obj): self.staffs.append(staff_obj) print("为%s 雇佣新员工" % staff_obj._name) class school_member(object): def __init__(self,name,age,sex): self._name=name self._age=age self._sex=sex def tell(self): print(‘‘‘ info of is %s name:%s age:%s sex:%s salary:%s course:%s ‘‘‘ % (self._name, self._age, self._sex, self._course, self._salary)) class teacher(school_member): def __init__(self,name,age,sex,salary,course): super(teacher,self).__init__(name,age,sex) self._salary=salary self._course=course def tell(self): print(‘‘‘ -------info of is %s--------- name:%s age:%s sex:%s salary:%s course:%s ‘‘‘%(self._name,self._name,self._age,self._sex,self._course,self._salary)) def teach (self): print("%s is teaching course[%s]"%(self._name,self._course)) class student(school_member): def __init__(self,name,age,sex,stu_id,grade): super(student,self).__init__(name,age,sex) self._stu_id=stu_id self._grade=grade def tell(self): print(‘‘‘ info of is %s name:%s age:%s sex:%s stu_id:%s grade:%s ‘‘‘ % (self._name,self._name, self._age, self._sex, self._stu_id, self._grade)) def pay_tuition(self,amount): print("%s jiao le $%s"%(self._name,amount)) t1=teacher("tom",20,"MF",777,"LINUX") t2=teacher("JONSON",20,"F",7707,"PYTHON") s1=student("zhangsan",20,"M",1001,"LINUX") s2=student("lisi",20,"M",1002,"python") SCHOOL=school("老男孩It","沙河") t2.tell() #显示教师信息 s1.tell() #显示学生信息 s2.pay_tuition(1000)#学生可以教学费 SCHOOL.enroll(s1)#学校为学生注册 SCHOOL.hire(t2)#学校雇佣老师 print(SCHOOL.students) print(SCHOOL.staffs) SCHOOL.staffs[0].teach() for stu in SCHOOL.students: stu.pay_tuition(5000)
class Animal: def __init__(self, name): # Constructor of the class self.name = name def talk(self): pass class Cat(Animal): def talk(self): print(‘Meow!‘) class Dog(Animal): def talk(self): print(‘Woof! Woof!‘) d1=Cat("zhangsan") d1.talk() d2=Dog("lisi") d2.talk() #另一种方法 class Animal: def __init__(self, name): # Constructor of the class self.name = name def talk(self): # Abstract method, defined by convention only #raise NotImplementedError("Subclass must implement abstract method") pass @staticmethod def animal_talk(obj): obj.talk() class Cat(Animal): def talk(self): print(‘Meow!‘) class Dog(Animal): def talk(self): print(‘Woof! Woof!‘) d1=Cat("zhangsan") d2=Dog("lisi") Animal.animal_talk(d1) Animal.animal_talk(d2)
继承的查找策略(广度优先--python3、深度优先python2)
#-*- coding:utf8 -*- class A(object): def __init__(self): print("A") class B (A): def __init__(self): print("B") class C (A): pass # def __init__(self): # print("C") class D(B,C): pass # def __init__(self): # # print("D") obj=D() #注意:
#python3中先D-B-C-A #python2中先D-B-A-C
以上是关于python day6 面向对象的主要内容,如果未能解决你的问题,请参考以下文章