面向对象编程 (OOP) 语言的一个主要功能就是“继承”。继承是指这样一种能力:它可以使用现有类的所有功能,并在无需重新编写原来的类的情况下对这些功能进行扩展。
通过继承创建的新类称为“子类”或“派生类”,被继承的类称为“基类”、“父类”或“超类”,继承的过程,就是从一般到特殊的过程。在某些 OOP 语言中,一个子类可以继承多个基类。但是一般情况下,一个子类只能有一个基类,要实现多重继承,可以通过多级继承来实现。
继承概念的实现方式主要有2类:实现继承、接口继承。
- 实现继承是指使用基类的属性和方法而无需额外编码的能力。
- 接口继承是指仅使用属性和方法的名称、但是子类必须提供实现的能力(子类重构爹类方法)。
继承:
1、继承时,子类会自动调用父类的构造函数
2、多继承:继承的父类会由左向右,一次继承构造第一个继承的父类无构造函数,则会找下一个父类的构造函数,第一个继承的父类有构造函数,那么在实例化子类时就要传相应多的参数,需要多传参数时,就需要重构父类
3、继承类的构造方法:
经典类的写法: 父类名称.__init__(self,参数1,参数2,...)
新式类的写法:super(子类,self).__init__(参数1,参数2,....)
4、方法:A类继承B,A类和B类有相同方法时,优先使用A本类的,A类没有时,才去父类B找,使用父类的
5、实例变量:A类继承B,A类和B类有相同实例变量时,优先使用A本类的,A类没有时,才去父类B找,使用父类的
6、类变量:A类继承B,A类和B类有相同类变量时,优先使用A本类的,A类没有时,才去父类B找,使用父类的
7、新式类按广度优选继承的,B、C分别继承A,D继承B、C 执行D,当D没有构造方法找B,当B没有找C,当C没有找A
#单类继承
class People(object):#新式类def __init__(self,name,age):self.name=nameself.age=agedef A(self):print("this is People类 A方法,姓名:%s,年龄:%s"%(self.name,self.age))
class Man(People):#继承People类def A(self):print("this is Man类A方法,姓名:%s,年龄:%s"%(self.name,self.age))def B(self):print("this is Man类B方法继承People类,姓名:%s,年龄:%s"%(self.name,self.age))
class Women(People):#继承People类def __init__(self,name,age,money):#先继承,再重构People.__init__(self,name,age)#继承父类构造方法#super(Women,self).__init__(name,age)#方法2self.money=moneydef B(self):People.A(self)print("this is Women类A方法,姓名:%s,年龄:%s,薪资:%s" % (self.name, self.age,self.money))
p=People("wangli",7)
p.A()
print(p.name,p.age)
m=Man("chengzi",11)
m.A()
m.B()
print(m.name,m.age)
w=Women("xixi",22,111111)
w.B()
print(w.name,w.age,w.money)C:\Users\wangli\PycharmProjects\AutoMation\venv\Scripts\python.exe C:/Users/wangli/PycharmProjects/AutoMation/case/test.py
this is People类 A方法,姓名:wangli,年龄:7
wangli 7
this is Man类A方法,姓名:chengzi,年龄:11
this is Man类B方法继承People类,姓名:chengzi,年龄:11
chengzi 11
this is People类 A方法,姓名:xixi,年龄:22
this is Women类A方法,姓名:xixi,年龄:22,薪资:111111
xixi 22 111111Process finished with exit code 0
#多重继承例1:class Person(object):def __init__(self,name,sex):self.name = nameself.sex = sexdef print_title(self):if self.sex == "male":print("man")elif self.sex == "female":print("woman")class Child(Person):passclass Baby(Child):passMay = Baby("May","female") # 继承上上层父类的属性
print(May.name,May.sex)
May.print_title() # 可使用上上层父类的方法class Child(Person): def print_title(self):if self.sex == "male":print("boy")elif self.sex == "female":print("girl")class Baby(Child):passMay = Baby("May","female")
May.print_title() # 优先使用上层类的方法
#多重继承例2:
class People(object):#新式类name='我是父类变量'def __init__(self,name,age):self.name=nameself.age=agedef A(self):print("this is People类 A方法,姓名:%s,年龄:%s"%(self.name,self.age))
class Relaton(object):def make_friends(self,obj):print("%s is making friends with %s" % (self.name, obj.name))
class Man(People,Relaton):#多重继承def A(self):print("this is Man类A方法,姓名:%s,年龄:%s"%(self.name,self.age))def B(self):print("this is Man类B方法继承People类,姓名:%s,年龄:%s"%(self.name,self.age))
class Women(People,Relaton):#多重继承name='我是Women类变量'def __init__(self,name,age,money):#先继承,再重构People.__init__(self,name,age)#继承父类构造方法self.money=moneydef B(self):People.A(self)print("this is Women类A方法,姓名:%s,年龄:%s,薪资:%s" % (self.name, self.age,self.money))
p=People("wangli",7)
p.A()
print(p.name,p.age)
m=Man("chengzi",20)
m.A()
m.B()
print(m.name,m.age)
w=Women("xixi",22,10000)
w.B()
print(w.name)
print(w.name,w.age,w.money)
print(People.name,Women.name)m.make_friends(w)#多重继承调用C:\Users\wangli\PycharmProjects\AutoMation\venv\Scripts\python.exe C:/Users/wangli/PycharmProjects/AutoMation/case/test.py
this is People类 A方法,姓名:wangli,年龄:7
wangli 7
this is Man类A方法,姓名:chengzi,年龄:20
this is Man类B方法继承People类,姓名:chengzi,年龄:20
chengzi 20
this is People类 A方法,姓名:xixi,年龄:22
this is Women类A方法,姓名:xixi,年龄:22,薪资:10000
xixi
xixi 22 10000
我是父类变量 我是Women类变量
chengzi is making friends with xixiProcess finished with exit code 0
#多重继承3新式类按广度优先继承构造函数的,B、C分别继承A,D继承B和C 实例化D,会调用执行构造函数,当D没有构造方法找B,当B没有找C,当C没有找A
---------------------------------------------------------
class A(object):pass
class B(A):pass
class C(A):pass
class D(B,C):def __init__(self,name):self.name=nameprint('D类有构造函数优先调本来的【%s】'%self.name)
d=D('橙子') #实例化会自动调构造函数C:\Users\wangli\PycharmProjects\AutoMation\venv\Scripts\python.exe C:/Users/wangli/PycharmProjects/AutoMation/case/test.py
D类有构造函数优先调本来的【橙子】-------------------------------------------------------
class A(object):pass
class B(A):def __init__(self,name):self.name=nameprint('D类无构造函数,找B类构造函数优先调用【%s】'%self.name)
class C(A):pass
class D(B,C):pass
d=D('橙子') #实例化会自动调构造函数C:\Users\wangli\PycharmProjects\AutoMation\venv\Scripts\python.exe C:/Users/wangli/PycharmProjects/AutoMation/case/test.py
D类无构造函数,找B类构造函数优先调用【橙子】------------------------------------------------------------class A(object):pass
class B(A):pass
class C(A):def __init__(self,name):self.name=nameprint('D类、B类无构造函数,找C类构造函数优先调用【%s】'%self.name)
class D(B,C):pass
d=D('橙子') #实例化会自动调构造函数C:\Users\wangli\PycharmProjects\AutoMation\venv\Scripts\python.exe C:/Users/wangli/PycharmProjects/AutoMation/case/test.py
D类、B类无构造函数,找C类构造函数优先调用【橙子】-----------------------------------------------------------------
class A(object):def __init__(self,name):self.name=nameprint('D类、B类、C类无构造函数,找A类构造函数优先调用【%s】'%self.name)
class B(A):pass
class C(A):pass
class D(B,C):pass
d=D('橙子') #实例化会自动调构造函数C:\Users\wangli\PycharmProjects\AutoMation\venv\Scripts\python.exe C:/Users/wangli/PycharmProjects/AutoMation/case/test.py
D类、B类、C类无构造函数,找A类构造函数优先调用【橙子】
-------------------------------------------------------------------
#继承实例4
class School(object):#学校类def __init__(self,name,addr):self.name=nameself.addr=addrself.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)print("雇用新员工%s"%staff_obj.name)
class SchoolMember(object):def __init__(self,name,age,sex):self.name=nameself.age=ageself.sex=sexdef tesll(self):pass
class Teacher(SchoolMember):def __init__(self,name,age,sex,salary,course):super(Teacher,self).__init__(name,age,sex)self.salary=salaryself.course=coursedef tell(self):print('''---- info of Teacher:%s ----Name:%sAge:%sSex:%sSalary:%sCourse:%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_idself.grade=gradedef tell(self):print('''---- info of Student:%s ----Name:%sAge:%sSex:%sStu_id:%sGrade:%s'''% (self.name, self.name, self.age, self.sex, self.stu_id, self.grade))def pay_tuition(self,amount):print("%s has paid tution for $%s" % (self.name, amount))
school=School("橙子IT","陕西")#实例化t1=Teacher("王力",20,"M",200000,"Linux")#实例化
t2=Teacher("王荔",22,"F",3000,"PtrhonDevOps")#实例化s1 = Student("橙汁",36,"MF",1001,"PythonDevOps")#实例化
s2 = Student("橘子",19,"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()#t1老师的实例.teach()
for stu in school.students:#遍历学生,调学生方法stu.pay_tuition(5000)C:\Users\wangli\PycharmProjects\AutoMation\venv\Scripts\python.exe C:/Users/wangli/PycharmProjects/AutoMation/case/test.py
---- info of Teacher:王力 ----Name:王力Age:20Sex:MSalary:200000Course:Linux
---- info of Student:橙汁 ----Name:橙汁Age:36Sex:MFStu_id:1001Grade:PythonDevOps
雇用新员工王力
为学员橙汁 办理注册手续
为学员橘子 办理注册手续
[<__main__.Student object at 0x000001BDCB69BCC0>, <__main__.Student object at 0x000001BDCB69BCF8>]
[<__main__.Teacher object at 0x000001BDCB69BC50>]
王力 is teaching course [Linux]
橙汁 has paid tution for $5000
橘子 has paid tution for $5000Process finished with exit code 0