面向对象:都可以分类;都是对象;只要是对象,就可以属于某种分类;只要是对象,具有一定的属性;
语法
特性
class
object
封装
继承
多态
属性
方法
构造函数
析构函数
私有方法,私有属性
面向过程&&面型对象
编程范式
编程是 程序 员 用特定的语法+数据结构+算法组成的代码来告诉计算机如何执行任务的过程 , 一个程序是程序员为了得到一个任务结果而编写的一组指令的集合,正所谓条条大路通罗马,实现一个任务的方式有很多种不同的方式, 对这些不同的编程方式的特点进行归纳总结得出来的编程方式类别,即为编程范式。 不同的编程范式本质上代表对各种类型的任务采取的不同的解决问题的思路, 大多数语言只支持一种编程范式,当然也有些语言可以同时支持多种编程范式。 两种最重要的编程范式分别是面向过程编程和面向对象编程。
面向过程
Procedural programming uses a list of instructions to tell the computer what to do step-by-step.
面向过程编程依赖 - 你猜到了- procedures,一个procedure包含一组要被进行计算的步骤, 面向过程又被称为top-down languages, 就是程序从上到下一步步执行,一步步从上到下,从头到尾的解决问题 。基本设计思路就是程序一开始是要着手解决一个大的问题,然后把一个大问题分解成很多个小问题或子过程,这些子过程再执行的过程再继续分解直到小问题足够简单到可以在一个小步骤范围内解决。
面向对象编程(oop)
OOP编程是利用“类”和“对象”来创建各种模型来实现对真实世界的描述,使用面向对象编程的原因一方面是因为它可以使程序的维护和扩展变得更简单,并且可以大大提高程序开发效率 ,另外,基于面向对象的程序可以使它人更加容易理解你的代码逻辑,从而使团队开发变得更从容。
面向对象的几个核心特性如下
Class 类
一个类即是对一类拥有相同属性的对象的抽象、蓝图、原型。在类中定义了这些对象的都具备的属性(variables(data))、共同的方法
Object 对象
一个对象即是一个类的实例化后实例,一个类必须经过实例化后方可在程序中调用,一个类可以实例化多个对象,每个对象亦可以有不同的属性,就像人类是指所有人,每个人是指具体的对象,人与人之前有共性,亦有不同
Encapsulation 封装
在类中对数据的赋值、内部调用对外部用户是透明的,这使类变成了一个胶囊或容器,里面包含着类的数据和方法
Inheritance 继承
一个类可以派生出子类,在这个父类里定义的属性、方法自动被子类继承
Polymorphism 多态
多态是面向对象的重要特性,简单点说:“一个接口,多种实现”,指一个基类中派生出了不同的子类,且每个子类在继承了同样的方法名的同时又对父类的方法做了不同的实现,这就是同一种事物表现出的多种形态。
编程其实就是一个将具体世界进行抽象化的过程,多态就是抽象化的一种体现,把一系列具体事物的共同点抽象出来, 再通过这个抽象的事物, 与不同的具体事物进行对话。
对不同类的对象发出相同的消息将会有不同的行为。比如,你的老板让所有员工在九点钟开始工作, 他只要在九点钟的时候说:“开始工作”即可,而不需要对销售人员说:“开始销售工作”,对技术人员说:“开始技术工作”, 因为“员工”是一个抽象的事物, 只要是员工就可以开始工作,他知道这一点就行了。至于每个员工,当然会各司其职,做各自的工作。
多态允许将子类的对象当作父类的对象使用,某父类型的引用指向其子类型的对象,调用的方法是该子类型的方法。这里引用和调用方法的代码编译前就已经决定了,而引用所指向的对象可以在运行期间动态绑定。
cs实例化
class Role(object): def __init__(self, name, 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("ah...,I got shot...") def buy_gun(self, gun_name): print("%s just bought %s" %(self.name,gun_name)) r1 = Role('Zhang', 'police', 'AK47') #把一个类变成一个具体对象的过程叫实例化(初始化一个类,造了一个对象) r1.got_shot() r1.buy_gun("b51") r2 = Role('Jack', 'terrorist', 'B22') #生成一个角色 #r1.buy_gun("AK47")解释
class Role(object): def __init__(self, name, 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("ah...,I got shot...") def buy_gun(self, gun_name): print("%s just bought %s" %(self.name,gun_name)) r1 = Role('Zhang', 'police', 'AK47') #把一个类变成一个具体对象的过程叫实例化(初始化一个类,造了一个对象) r1.got_shot() r1.buy_gun("b51") r2 = Role('Jack', 'terrorist', 'B22') #生成一个角色 #r1.buy_gun("AK47")改变2
class Role(object): n = 123 #类变量 neme = "我是类name" def __init__(self, name, 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("ah...,I got shot...") def buy_gun(self, gun_name): print("%s just bought %s" %(self.name,gun_name)) r1 = Role('Zhang', 'police', 'AK47') #把一个类变成一个具体对象的过程叫实例化(初始化一个类,造了一个对象) r1.name = "zhangguo" r1.bullet_prove = True r1.n = "该类变量" print(r1.n,r1.name) #r1.got_shot() #r1.buy_gun("b51") r2 = Role('Jack', 'terrorist', 'B22') #生成一个角色 r2.name="qi" print(r2.n,r2.name) #r1.buy_gun("AK47")
类的变量的用途,大家公用的属性;节省空间;
#大家共有的属性;节约空间; class Person: cn = "中国" def __init__(self,name,age,addr,cn="china") self.name = name self.cn = cn
析构函数,在实例释放、销毁的时候执行的,通常用于一些收尾工作,如关闭一些数据库连接,打开的临时文件;
私有属性;私有方法(变量);
class Role(object): n = 123 #类变量 neme = "我是类name" def __init__(self, name, 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 __del__(self): #析构方法 #print("%s 彻底死亡。。。"%self.name) def shot(self):#类的方法,功能(动态属性) print("shooting...") def show_status(self): print("name:%s weapen:%s life_val:%s"%(self.name,self.weapon,self._life_value)) def got_shot(self): self._life_value -=50 print("%s:ah...,I got shot..."%self.name) def buy_gun(self, gun_name): print("%s just bought %s" %(self.name,gun_name)) r1 = Role('zhang', 'police', 'AK47') #把一个类变成一个具体对象的过程叫实例化(初始化一个类,造了一个对象) r1.name = "ALEX" r1.bullet_prove = True #r1.n = "该类变量" print(r1.n,r1.name) #r1.got_shot() #r1.buy_gun("b51") r2 = Role('Jack', 'terrorist', 'B22') #生成一个角色 r2.got_shot() print(r1.show_status())
封装;继承;多态;
继承:
class People: def __init__(self,name,age): self.name = name self.age = age def eat(self): print("%s is eating..." % self.name) def talk(self): print("%s is talking..." % self.name) def sleep(self): print("%s is sleeping..." %self.name) class Men(People): def piao(self): print("%s is piaoing....20s...done" %self.name) m1 = Men("zhang",16) m1.eat() m1.piao()
例2
class People: def __init__(self,name,age): self.name = name self.age = age def eat(self): print("%s is eating..." % self.name) def talk(self): print("%s is talking..." % self.name) def sleep(self): print("%s is sleeping..." %self.name) class Men(People): def piao(self): print("%s is piaoing....20s...done" %self.name) def sleep(self): People.sleep(self) print("man is sleeping") m1 = Men("zhang",16) m1.eat() m1.piao() m1.sleep()
例3
class People: def __init__(self,name,age): self.name = name self.age = age def eat(self): print("%s is eating..." % self.name) def talk(self): print("%s is talking..." % self.name) def sleep(self): print("%s is sleeping..." %self.name) class Men(People): def piao(self): print("%s is piaoing....20s...done" %self.name) def sleep(self): People.sleep(self) print("man is sleeping") class Woman(People): def get_birth(self): print("%s is born a baby...." %self.name) m1 = Men("zhang",16) m1.eat() m1.piao() m1.sleep() w1 = Woman("Cheng",26) w1.get_birth()
结果:
例4(重构)
class People: def __init__(self,name,age): self.name = name self.age = age def eat(self): print("%s is eating..." % self.name) def talk(self): print("%s is talking..." % self.name) def sleep(self): print("%s is sleeping..." %self.name) class Men(People):#重构 def __init__(self,name,age,money): People.__init__(self,name,age) self.money = money print("%s 一出生就有%s money" %(self.name,self.money)) def piao(self): print("%s is piaoing....20s...done" %self.name) def sleep(self): People.sleep(self) print("man is sleeping") class Woman(People): def get_birth(self): print("%s is born a baby...." %self.name) m1 = Men("zhang",16,100) m1.eat() m1.piao() m1.sleep() w1 = Woman("Cheng",26) w1.get_birth()
例5(多继承)
class People(object): #新式类 def __init__(self,name,age): self.name = name self.age = age def eat(self): print("%s is eating..." % self.name) def talk(self): print("%s is talking..." % self.name) def sleep(self): print("%s is sleeping..." %self.name) class Relation(object): def make_friends(self, obj): print("%s is making friends with %s" % (self.name, obj.name)) class Men(People,Relation): def __init__(self,name,age,money): #People.__init__(self,name,age) super(Men,self).__init__(name,age)#作用同上 #新式类的写法 self.money = money print("%s 一出生就有%s money" %(self.name,self.money)) def piao(self): print("%s is piaoing....20s...done" %self.name) def sleep(self): People.sleep(self) print("man is sleeping") class Woman(People,Relation): def get_birth(self): print("%s is born a baby...." %self.name) #class Relation(object): #def make_friends(self, obj): #print("%s is making friends with %s" % (self.name, obj.name)) m1 = Men("zhang",16,100) #m1.eat() #m1.piao() #m1.sleep() w1 = Woman("Cheng",26) #w1.get_birth() m1.make_friends(w1)
经典类和新式类:最大不同,多继承上的顺序问题;
python2经典类是按深度优先来继承的,新式类按广度优先来继承的;
python3经典类和新式类都是统一按广度优先来继承的;
例子:
class A: def __init__(self): print("A") class B(A): def __init__(self): print("B") class C(A): def __init__(self): print("C") class D(B,C): pass #def __init__(self): #print("D") obj=D()
实例:
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): 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 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_tution(self,amount): print("%s has paid tution for $%s"%(self,amount)) school = School("老男孩IT","沙河") t1 = Teacher("Oldboy",66,"MF",2000000,"Linux") t2 = Teacher("ALEX",22,"M",2000,"PythonDEvOps") s1 = Student("ChenRong",36,"M",1001,"PythonDEvOps") s2 = Student("Xuliang",22,"M",1002,"Linux") t1.tell() s1.tell()
结果:
------info of Teacher:Oldboy----
Name:Oldboy
Age:66
Sex:MF
Salary:2000000
Course:Linux
------info of Student:ChenRong----
Name:ChenRong
Age:36
Sex:M
stu_id:1001
Grade:PythonDEvOps
终极版:
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.name) 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 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_tution(self,amount): print("%s has paid tution for $%s"%(self,amount)) school = School("老男孩IT","沙河") t1 = Teacher("Oldboy",66,"MF",2000000,"Linux") t2 = Teacher("ALEX",22,"M",2000,"PythonDEvOps") s1 = Student("ChenRong",36,"M",1001,"PythonDEvOps") s2 = Student("Xuliang",22,"M",1002,"Linux") t1.tell() s1.tell() school.hire(t1) school.enroll(s1) school.enroll(s2) print(school.students) print(school.staffs) school.staffs[0].teach() for stu in school.students: stu.pay_tution(5000)
运行结果: ------info of Teacher:Oldboy---- Name:Oldboy Age:66 Sex:MF Salary:2000000 Course:Linux ------info of Student:ChenRong---- Name:ChenRong Age:36 Sex:M stu_id:1001 Grade:PythonDEvOps 雇佣新员工Oldboy 为学员ChenRong 办理注册手续 为学员Xuliang 办理注册手续 [<__main__.Student object at 0x05725970>, <__main__.Student object at 0x05725990>] [<__main__.Teacher object at 0x05725930>] Oldboy is teaching course [Linux] <__main__.Student object at 0x05725970> has paid tution for $5000 <__main__.Student object at 0x05725990> has paid tution for $5000
多态:
一种接口;多种实现;
多态性(polymorphisn)是允许你将父对象设置成为和一个或更多的他的子对象相等的技术,赋值之后,父对象就可以根据当前赋值给它的子对象的特性以不同的方式运作。简单的说,就是一句话:允许将子类类型的指针赋值给父类类型的指针。
class Animal(object): 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") class Cat(Animal): def talk(self): print('%s: 喵喵喵!' % self.name) class Dog(Animal): def talk(self): print('%s: 汪!汪!汪!' % self.name) def func(obj): # 一个接口,多种形态 obj.talk() c1 = Cat('ABC') #c1.talk() d1 = Dog('CAD') #d1.talk() func(c1) func(d1)