2018.7.26-------面向对象-------------------- 编程范式 1》面向对象 2》面向过程 3》函数式编程 面向对象的几个特性:继承 封装 多态 @Role.py class Role: #类变量 n=123 name="我是类name" def __init__(self,name,role,weapon,life_value=100,money=15000): #构造函数 #在实例化的时候传参 同时将r1传入 与一般函数传值不同 r1=self self.name=name #实例变量(静态属性) 作用域实例本身 self.role=role self.weapon=weapon self.life_value=life_value self.money=15000 def shot(self): #类的方法,功能(动态属性) print("shotting") def got_shot(self): print("ah....,i got shot..") def buy_gun(self,gun_name): print("%s just bought %s" %(self.name,gun_name)) r1=Role('alex','police','ak47') #存在变量里 在内存里 同时将r1传入 与一般函数传值不同 r1.buy_gun("ak555") #Role.buy_gun(r1) r1=self r2=Role('Jack','terrrorist','ak47') #r2叫做Role的实例 r1.name="susong" #修改属性 #添加新属性 =====self.bullet_prove (r1特有 r2不可调用) r1.bullet_prove= True del r1.weapon print(r1.name) #先找实例变量 再找类变量 r1.n="修改类变量" print(r1.n) # 只会修改自己的类变量 实际上在r1里添加一个n print(r2.n) Role.n="ABC" print(r1.n,r2.n) #只有r2会影响 ------------------------------------------- 析构函数 #析构函数:在实例释放,销毁的时候,通常用于收尾工作 如关闭数据库打开的 临时链接 def __del__(self): #释放时自动执行 print("%s 彻底死了。。。"%self.name) -------------------------------------------------- 私有属性 私有方法一样 @ self.__life_value = life_value class Role: name = "我是class name" def __init__(self, name,role,weapon,life_value=1000,money=15000): self.name = name self.role = role self.weapon = weapon self.__life_value = life_value self.money = 15000 # def __del__(self): # 释放时自动执行 # print("%s 彻底死了。。。" % self.name) # pass def show_status(self): print("name:%s weapon:%s life_val:%s" % (self.name,self.weapon,self.__life_value)) def shot(self): print("shotting") def got_shot(self): self.__life_value-=50 print("ah....,i got shot..") def buy_gun(self, gun_name): print("%s just bought %s" % (self.name, gun_name)) r1 = Role('alex', 'police', 'ak47') # 存在变量里 在内存里 r1.buy_gun("ak555") # Role.buy_gun(r1) r1=self print(r1.show_status()) r1.got_shot() r1.show_status() --------------------重构父类的方法---------------------- class People(object): 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 talk(self): print("%s is talking..." % self.name) class Man(People): def __init__(self,name,age,money): # People.__init__(self,name,age) super(Man, self).__init__(name,age) self.money=money print("%s 一出生就有钱 %smoney "%(self.name,self.money)) def piao(self): print("%s is piaoing....20s...over."%self.name) def sleep(self): People.sleep(self) #重构父类方法 print("%s man is sleeping..." % self.name) class Women(People): def get_birth(self): print("%s is boring baby "%self.name) m1=Man("anyang",22) m1.eat() m1.piao() m1.sleep() w1=Women("shabia",25) w1.get_birth() ---------------------多继承---------------------- class People(object): 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 talk(self): print("%s is talking..." % self.name) class Relation(object): def make_friends(self,obj): print("%s is making frineds with %s" %(self.name,obj.name)) #继承people时name已经有了 class Man(People,Relation): #子类 多继承 def __init__(self,name,age,money): # People.__init__(self,name,age) 先执行我自己的name super(Man, self).__init__(name,age) self.money=money print("%s 一出生就有钱 %smoney "%(self.name,self.money)) def piao(self): print("%s is piaoing....20s...over."%self.name) def sleep(self): People.sleep(self) #重构父类方法 print("%s man is sleeping..." % self.name) class Women(People,Relation): def get_birth(self): print("%s is boring baby "%self.name) m1=Man("anyang",22,10) w1=Women("shabia",25) m1.make_friends(w1) ---------------------------新式类多继承----------- 继承策略:广度优先(横向)py3 深度优先 py2 py2经典类是深度优先继承的,新式类广度优先继承 py3都是按广度优先继承 ------------------------------------------------------- class School(object): def __init__(self,name,addr): self.name=name self.addr=addr self.students=[] #格式化一个新列表 self.staffs=[] def enroll(self,stu_obj): print("为学员%s办理注册手续"%stu_obj.name) self.students.append(stu_obj) def hire(self,staff_obj): print("雇佣新员工%s" % staff_obj) self.staffs.append(staff_obj) class SchoolMember(object): def __init__(self,name,age,sex,): self.name=name self.age=age self.sex=sex def tell(self): pass class Teacher(SchoolMember): 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 Teacher:%s------------- Name:%s Age:%s Sex:%s Salary:%s Course:%s """ % (self.name, self.name, self.age, self.sex, self.salary, self.course)) def teach(self): print("%s is teache [%s]" % (self.name, self.course)) class Student(SchoolMember): 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 Student:%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 has paid tuition for $%s"%(self.name,amount)) school=School("laonanhai","shanxi") t1=Teacher("Oldboy",22,"MF",2000,"Linux") t2=Teacher("song",20,"M",2000,"Python") s1=Student("Bai",20,"Mf",1001,"Python") s2=Student("xiaoBai",21,"Mf",1002,"Linux") t1.tell() s1.tell() school.hire(t1) school.enroll(s1) school.enroll(s2) school.staffs[0].teach() for stu in school.students: stu.pay_tuition(5000) ------------------------------多态----------------- 一种接口,多种实现 python不直接支持多态,但可以哦间接实现 class Animal: def __init__(self,name): self.name=name def talk(self): pass @staticmethod #略 def animal_talk(obj): obj.talk() class Cat(Animal): def talk(self): print('Meow!') class Dog(Animal): def talk(self): print('Wangwang!') d=Dog("zhangdongyu") d.talk() c=Cat("shabia") Animal.animal_talk(c) -----------------------------------------
python封装 继承 多态
猜你喜欢
转载自blog.csdn.net/weixin_41752144/article/details/81235639
今日推荐
周排行