1.Python面向对象编程OOP(Object Oriented Programming)
封装:可以隐藏实现细节,使代码模块化
继承:可以扩展已存在的代码模块,可以使代码实现重用
多态:一种接口,多种实现,实现接口重用,为了类在继承和派生的时候,保证家谱中任一类的实例的某一属性的正确调用。
python不支持多态,但可以间接实现。
# 新式类与经典类的差别主要在多继承的顺序问题 # class People: #经典类的写法 class People(object): # 新式类的写法 cn="中国"#类变量,大家公共的属性,节省内存开销 def __init__(self,name,age):#构造函数,在实例化时做一些类的初始化的工作 self.name=name#实例变量(静态变量),作用域就是实例本身 self.age=age self.lover=[] def eat(self):#类的方法,功能(动态属性) print("%s is eating......"%self.name) def sleep(self): print("%s is sleeping.... "%self.name) def __del__(self):#析构函数:在实例释放、销毁的时候自动执行,通常用于做一些收尾的工作,如关闭一些数据库连接,打开的临时文件 print("deleting %s"%self.name) class Animal(object): def walk(self): print('%s is able to walk.'%self.name) def make_love(self,obj): print('%s is making love with %s'%(self.name,obj.name)) self.lover.append(obj) # python中可以实现多继承,先后继承的还是有顺序的 class Man(People,Animal): def __init__(self,name,age,property,education):#子类中重构父类的构造方法 # People.__init__(self,name,age,education) super(Man,self).__init__(name,age)#这是新式类的写法,两种写法都可以的,下面这种更好, self.property=property self.__education=education#私有属性,定义时用__property样式, print('%s一出生就拥有%d元'%(self.name,self.property)) # 定义一个显示教育信息的方法 def show_edu(self): print("%s education :%s"%(self.name,self.__education)) def runfast(self):#在子类中添加新的方法 print("%s is running fast...."%self.name) def sleep(self): People.sleep(self)#执行父类的方法 print("Man is sleeping,,,,,")#添加新的功能 class Woman(People,Animal): def born(self): print("%s is borning a baby,,,,"%self.name) m1=Man("Jack",22,10000,"大学本科") #print(m1.__education)#会报错,私有属性不能这么访问, m1.show_edu()#通过函数进行访问 m1.sleep() w1=Woman("Rose",18) w1.sleep() m1.make_love(w1) w1.name='Roseenrr' print('%s has lover : %s'%(m1.name,m1.lover[0].name)) w1.born()
#python2.x中多继承:经典类按深度优先继承,新式类按广度优先继承的 #python3.x中多继承:都统一采用的广度优先继承 #多继承实例,B,C继承A,D继承B、C class A(object): # pass def __init__(self): print("A") class B(A): # pass def __init__(self): print('B') class C(A): # pass def __init__(self): print("C") class D(B,C): # pass def __init__(self): print("D") d1=D()#构造函数__init__()只构造一次,搜索顺序:D自身-->B--->C--->A
class School(object): def __init__(self,name,addr): self.name=name self.addr=addr self.students=[] self.teachers=[] def enroll(self,stu_obj): print("正在为学员%s办理入学注册手续"%stu_obj.name) self.students.append(stu_obj) def hire(self,staff_obj): print("雇佣新的教职工:%s"%staff_obj.name) self.teachers.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 teaching course:%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 缴费了 %s元'%(self.name,amount)) # 对象实例化 school=School("SMU","Shanghai") teacher1=Teacher("Jackson",48,'M',15000,"C语言") teacher2=Teacher("Jack",55,'M',18000,"python语言编程") s1=Student("Jean",22,'M',2017001,"2班") s2=Student("Alice",18,'F',2017002,"1班") s1.tell() s2.tell() school.hire(teacher1) school.hire(teacher2) school.enroll(s1) school.enroll(s2) print(school.students) print(school.teachers) school.teachers[0].teach() for stu in school.students: stu.pay_tuition(5000)
#多态的实例 class Animal(object): def __init__(self,name): self.name=name def howl(self): pass #实现多态的方法 @staticmethod def animal_howl(obj): obj.howl() class Cat(Animal): def howl(self): print("Meow,Meow...") class Dog(Animal): def howl(self): print("WOw,Wow,Wow") c=Cat("Tom") d=Dog("Wory") Animal.animal_howl(c)#同样的方法,传入不同的对象,执行不同的方法 Animal.animal_howl(d)