(编辑:jimmy 日期: 2024/11/16 浏览:2)
本文实例讲述了Python3.5面向对象程序设计之类的继承和多态。分享给大家供大家参考,具体如下:
1、继承的定义
继承是指:可以使用现有类的所有功能,并在无需重新编写原来的类的情况下对这些功能进行扩展。
(1)通过继承创建的新类称为“子类”或“派生类”。
(2)被继承的类称为“基类”、“父类”或“超类”。
继承的过程,就是从一般到特殊的过程。要实现继承,可以通过“继承”(Inheritance)和“组合”(Composition)来实现。
在某些 OOP 语言中,一个子类可以继承多个基类。但是一般情况下,一个子类只能有一个基类,要实现多重继承,可以通过多级继承来实现。
2、继承的分类
继承概念的实现方式主要有2类:实现继承、接口继承。
(1) 实现继承是指使用基类的属性和方法而无需额外编码的能力;
(2)接口继承是指仅使用属性和方法的名称、但是子类必须提供实现的能力(子类重构父类方法);
在考虑使用继承时,有一点需要注意,那就是两个类之间的关系应该是“属于”关系。
抽象类仅定义将由子类创建的一般属性和方法。
OO开发范式大致为:划分对象→抽象类→将类组织成为层次化结构(继承和合成) →用类与实例进行设计和实现几个阶段。
3、示例代码
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:ZhengzhengLiu #类的继承 class People: 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): #继承父类People类 def make_money(self): print("%s is making money..." %self.name) def sleep(self): People.sleep(self) #对父类方法的扩展 print("man is sleeping...") class Women(People): def shop(self): print("%s is shopping..." %self.name) m1 = Man("Jack","20") m1.eat() m1.make_money() m1.sleep() w1 = Women("Amy","25") w1.talk() w1.shop()
运行结果:
Jack is eating...
Jack is making money...
Jack is sleeping...
man is sleeping...
Amy is talking...
Amy is shopping...
4、子类中对父类的构造函数进行重构两种方法
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:ZhengzhengLiu #类的继承 #class People: #经典类 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): #继承父类People类 def __init__(self,name,age,money): #People.__init__(self,name,age) #(方法一)对构造函数进行重构、添加父类中没有的属性 super(Man,self).__init__(name,age) #(方法二)利用super对构造函数进行重构(新式类写法) self.money = money print("%s have money %s$" %(self.name,self.money)) def make_money(self): print("%s is making money..." %self.name) def sleep(self): People.sleep(self) #对父类方法的扩展 print("man is sleeping...") class Women(People): def shop(self): print("%s is shopping..." %self.name) m1 = Man("Jack","20",10) m1.eat() m1.make_money() m1.sleep() w1 = Women("Amy","25") w1.talk() w1.shop()
运行结果:
J ack have money 10$
Jack is eating...
Jack is making money...
Jack is sleeping...
man is sleeping...
Amy is talking...
Amy is shopping...
5、多继承方式
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:ZhengzhengLiu #类的继承 #class People: #经典类 class People(object): #新式类 def __init__(self,name,age): self.name = name self.age = age self.friends = [] 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 Relationship(object): def make_friends(self,obj): print("%s is making friends with %s" %(self.name,obj.name)) self.friends.append(obj) class Man(People,Relationship): #多继承 def __init__(self,name,age,money): #People.__init__(self,name,age) #(方法一)对构造函数进行重构、添加父类中没有的属性 super(Man,self).__init__(name,age) #(方法二)利用super对构造函数进行重构(新式类写法) self.money = money print("%s have money %s$" %(self.name,self.money)) def make_money(self): print("%s is making money..." %self.name) def sleep(self): People.sleep(self) #对父类方法的扩展 print("man is sleeping...") class Women(People,Relationship): #多继承 def shop(self): print("%s is shopping..." %self.name) m1 = Man("Jack","20",10) w1 = Women("Amy","25") m1.make_friends(w1) w1.name = "liu" print(m1.friends)
运行结果:
Jack have money 10$
Jack is making friends with Amy
[<__main__.Women object at 0x0057FA30>]
6、新式类与经典类的继承顺序
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:ZhengzhengLiu class A(object): #新式类 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): def __init__(self): pass #print("D") obj = D()
7、继承示例——学校、教师与学生
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:ZhengzhengLiu #继承实例(新式类)——模拟学校、教师与学生 class School(object): def __init__(self,name,addr): self.name = name self.addr = addr self.students = [] self.stuffs = [] def enroll(self,stu_obj): #学生注册 print("%s 学员办理注册" %stu_obj.name) self.students.append(stu_obj) def heir(self,staff_obj): #聘请教师 print("聘请教师 %s" %staff_obj.name) self.stuffs.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 has paied tuition for $%s" %(self.name,amount)) #实例化 school = School("qinghua","beijing") t1 = Teacher("Jack","30","M","20000","Python") t2 = Teacher("Amy","28","F","15000","Linux") s1 = Student("liu","23","M","1701","Python") s2 = Student("wang","25","F","1702","Linux") #调用显示学生与教师的信息 t1.tell() s1.tell() school.heir(t1) #聘请教师t1 school.enroll(s1) #学生s1注册 school.enroll(s2) print(school.stuffs) print(school.students) #聘请的第一位教师教课 school.stuffs[0].teach() for stu in school.students: stu.pay_tuition(5000)
运行结果:
----- info of Teacher:Jack -----
Name:Jack
Age:30
Sex:M
Salary:20000
Course:Python
----- info of Student:liu -----
Name:liu
Age:23
Sex:M
Stu_id:1701
Grade:Python
聘请教师 Jack
liu 学员办理注册
wang 学员办理注册
[<__main__.Teacher object at 0x0059FDB0>]
[<__main__.Student object at 0x0059FDF0>, <__main__.Student object at 0x0059FE10>]
Jack is teaching course [Python]
liu has paied tuition for $5000
wang has paied tuition for $5000
8、多态(polymorphisn)——一种接口,多种形态
(1)定义
多态性(polymorphisn)是允许你将父对象设置成为和一个或更多的他的子对象相等的技术,
赋值之后,父对象就可以根据当前赋值给它的子对象的特性以不同的方式运作。
简单的说,就是一句话:允许将子类类型的指针赋值给父类类型的指针。
多态的作用:我们知道,封装可以隐藏实现细节,使得代码模块化;继承可以扩展已存在的代码模块(类);它们的目的都是为了——代码重用。
而多态则是为了实现另一个目的——接口重用!多态的作用,就是为了类在继承和派生的时候,保证使用“家谱”中任一类的实例的某一属性时的正确调用。
Pyhon 很多语法都是支持多态的,比如 len(),sorted(), 你给len传字符串就返回字符串的长度,传列表就返回列表长度。
(2)示例代码:
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:ZhengzhengLiu class Animal(object): def __init__(self,name): self.name = name def talk(self): raise NotImplementedError("Subclass must implement abstract method") # 多态——一种接口,多种形态 @staticmethod def animal_talk(obj): obj.talk() class Cat(Animal): def talk(self): print("%s Meow!" %self.name) class Dog(Animal): def talk(self): print("%s Woof! Woof!" % self.name) d = Dog("A") #d.talk() c = Cat("B") #c.talk() #多态 Animal.animal_talk(d) Animal.animal_talk(c)
运行结果:
A Woof! Woof!
B Meow!
9、面向对象设计利器——领域建模
(1)定义
从领域模型开始,我们就开始了面向对象的分析和设计过程,可以说,领域模型是完成从需求分析到面向 对象设计的一座桥梁。
领域模型,顾名思义,就是需求所涉及的领域的一个建模,更通俗的讲法是业务模型。
(2)领域模型有两个主要的作用:
发掘重要的业务领域概念
建立业务领域概念之间的关系
(3)领域建模三字经
领域模型如此重要,领域建模的方法概括一下就是“找名词”! 即使是简单的找名词这样的操作,也涉及到分析和提炼,而 不是简单的摘取出来就可,
这种情况下分析师和设计师的经验和技能就能够派上用场了。但领域模型分析 也确实相对简单,即使没有丰富的经验和高超的技巧,至少也能完成一个能用的领域模型。
一个关键的问题:从哪里找"" src="/UploadFiles/2021-04-08/2019042410560174.png">
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python面向对象程序设计入门与进阶教程》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python编码操作技巧总结》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。