继承介绍
1 什么是继承
继承一种新建类的方式,新建的类称之为子类/派生类,被继承的类称之为父类\基类\超类
python中继承的特点:
1-1. 子类可以遗传/重用父类的属性
1-2. 利用继承来解决类和类之间的代码冗余问题
2 为何要用继承
减少类与类之间代码冗余
总结对象的相似之处得到了类
总结类的相似之处得到父类
3 如何用继承
class People:
school = '图灵学院'
def __init__(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex
class Student(People):
def play(self):
print('%s play football'%self.name)
class Teacher(People):
def course(self):
print('%s course'%self.name)
# 实例化的时候子类没有__init__方法会调用父类的
stu1 = Student('周阳',30,'male')
print(stu1.__dict__)
tea1 = Teacher('大海',31,'man')
print(tea1.__dict__)
子类派生的新方法中重用父类功能的方式一
子类重用父类的功能
在子类派生出的新方法中重用父类功能的方式一:
指名道姓地引用某一个类中的函数
class People:
school = '图灵学院'
def __init__(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex
class Student(): #可以不加父类名
# 写个默认参数
def __init__(self, name, age, sex,score=0):
**父类.函数**
People.__init__(self,name,age,sex)
**派生的新方法**
self.score = score
def play(self):
print('%s play football'%self.name)
class Teacher(): #可以不加父类名
def __init__(self, name, age, sex,hobby):
**父类.函数**
People.__init__(self, name, age, sex)
**派生的新方法**
self.hobby = hobby
def course(self):
print('%s course'%self.name)
#实例化的时候子类没有__init__方法会调用父类的
stu1 = Student('周阳',30,'male')
print(stu1.__dict__)
stu1.play()
tea1 = Teacher('大海',20,'man','篮球')
print(tea1.__dict__)
在子类派生的新方法中重用父类功能的方式二
派生实例化除了父类的属性添加,还能有自己独有的属性 ******
在子类派生出的新方法中重用父类功能的方式二:super()必须在类中用
super(自己的类名,自己的对象)
可以省略传值
super()
class People:
school = '图灵学院'
def __init__(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex
class Student(People): #必须加父类名
# 写个默认参数
def __init__(self, name, age, sex,score=0): #必须写__init__(self,参数```)
# 必须要继承 Student父类的
# super().函数名
super().__init__(name, age, sex): #必须写super().__init__(参数```)
**派生的新方法**
self.score = score
def play(self):
print('%s play football'%self.name)
class Teacher(People): #必须加父类名
def __init__(self, name, age, sex,hobby):
# super().函数名
super().__init__(name, age, sex)
**派生的新方法**
self.hobby = hobby
def course(self):
print('%s course'%self.name)
stu1 = Student('周阳',30,'male',99)
print(stu1.__dict__)
tea1 = Teacher('大海',20,'man','篮球')
print(tea1.__dict__)
组合
-
什么是组合
组合指的是某一个对象拥有一个属性,该属性的值是另外一个类的对象 *****
组合和继承的区别,继承是把全部的属性和方法让子类可以调用,而组合只是部分的属性和方法把2个类关联到一起,有些类的属性不能全部继承,这就用到了组合,组合的是对象,而继承的是,类继承是在类定义产生的,它是类之间的关联关系,而组合是类定义后产生的关系,因为它是对象的关联关系 -
为何要用组合
通过为某一个对象添加属性(属性的值是另外一个类的对象)的方式,可以间接地将两个类关联/整合/组合到一起
从而减少类与类之间代码冗余 -
如何用组合
class People: school = '图灵学院' def __init__(self,name,age,sex): self.name = name self.age = age self.sex = sex class Student(People): def __init__(self, name, age, sex,score=0): super().__init__(name, age, sex) self.score = score class Teacher(People): def __init__(self, name, age, sex,course = []): super().__init__(name, age, sex) self.course = course # print(course) def tell_info1(self): # 这个时候是列表里面装了3个课程对象 # print(self.course) for i in self.course: # pass # 课程属性 # print('<课程名:%s,价格%s>'%(i.c_name,i.c_price)) # # 课程方法 i.tell_info() # print(i) class Course: def __init__(self,c_name,c_price): self.c_name = c_name self.c_price = c_price def tell_info(self): print('<课程名:%s,价格%s>'%(self.c_name,self.c_price)) python = Course('python开发',10000) math = Course('高等数学',3000) English = Course('英语',4000) music = Course('音乐',5000) drawing = Course('绘画',6000) tea = Teacher('大海',24,'man') #把music课程对象添加到老师对象tea的属性里面 tea.course = music tea.course.tell_info() tea.course = math tea.course.tell_info() #如果我想把一堆的对象放到一个对象属性里面,我们怎么做? #思考一下? 放到一个容器里面 列表 print(tea.course) tea.course.append(python) tea.course.append(math) tea.course.append(music) #这个时候是列表里面装了3个课程对象 print(tea.course) print('========') tea.tell_info1()
多态
-
什么是多态 ***
多态指的是同一种/类事物的不同形态 -
为何要用多态
多态性:在多态的背景下,可以在不用考虑对象具体类型的前提下而直接使用对象
多态性的精髓:统一 -
如何用多态
class Animal: # 动物都会叫 def speak(self): print('我是动物我会说话') class People(Animal): def speak(self): print('say hello') class Dog(Animal): def speak(self): print('汪汪汪') class Pig(Animal): def speak(self): print('哼哼哼') # 三个对象都是动物 obj1=People() obj2=Dog() obj3=Pig() # 多态性的精髓:统一 obj1.speak() obj2.speak() obj3.speak()
常用的魔法方法
str: 在对象被打印时自动触发,可以用来定义对象被打印时的输出信息(主动技能)
del 在对象被删除时先自动触发该方法,可以用来回收对象以外其他相关资源,比如系统资源
call: 在对象被调用时会自动触发该方法(被动技能)
用法:
str(self):
class People:
def __init__(self,name,age):
self.name = name
self.age = age
def __str__(self):
return '<name:%s age%s>'%(self.name,self.age)
obj = People('大海',18)
print(obj)
del(self):
class Foo:
def __init__(self,x,filepath,encoding = 'utf-8'):
self.x = x
self.f = open(filepath,'rt',encoding=encoding)
def __del__(self):
print('__del__正在运行')
self.f.close()
obj = Foo(1,'a.txt')
obj.f.read()
print('不是对象生成运行__del__,对象正在使用的时候不会运行')
call(self):
class Foo:
def __init__(self,name,age):
self.name = name
self.age = age
def __call__(self, *args, **kwargs):
print(self,args,kwargs)
def run(self):
print('run')
obj1 = Foo(1,2)
obj1.run()
obj1()
obj1(1,2,x=3,y=4)
省略了
obj1.__call__(1,2,x=3,y=4)