面向对象的特征
面向对象的特征之一:继承
什么是继承?
继承是一种创建新类的方式,新建的类可以继承一个或多个父类(python支持多继承),父类又可称为基类或超类,新建的类称为派生类或子类。
子类会继承父类的属性,也可使用父类的方法,从而解决代码冗余问题
python中类的继承分为单继承和多继承
# 单继承
# 以学生选课系统为例
# 父类,公共类
class People():
school = 'SH'
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
# 学生类
class Student(People):
def __init__(self, name, age, gender, course=None):
if course is None:
course = []
People.__init__(self, name, age, gender)
self.courses = course
def choose_course(self, course):
self.courses.append(course)
print('%s 选课成功 %s' % (self.name, self.courses))
stu = Student('tony', 19, 'male')
# teacher类
class Teacher(People):
def __init__(self, name, age, gender, level):
self.level = level
People.__init__(self, name, age, gender)
def score(self, stu_obj, score):
stu_obj.score = score # 给学生打分
print('%s给%s打了%s分' % (self.name, stu_obj.name, score))
tea = Teacher('lucy', 28, 'male', 10)
print(tea.name)
print(tea.level)
单继承下属性查找
class Foo:
def f1(self):
print('Foo.f1')
def f2(self):
#
print('Foo.f2')
self.f1()
class Bar(Foo):
def f1(self):
print('Bar.f1')
obj = Bar() # {}
obj.f2()
# 练习
class Foo:
def __f1(self): # _Foo__f1()
print('Foo.f1')
def f2(self):
#
print('Foo.f2')
self.__f1() # _Foo__f1()
class Bar(Foo):
def __f1(self): # # _Bar__f1()
print('Bar.f1')
obj = Bar() # {}
obj.f2()
多继承下属性查找
# 新式类:按照广度优先查询
# 经典类:按照深度优先查询
# python3中都是新式类
class A(object):
def test(self):
print('from A')
class B(A):
# def test(self):
# print('from B')
pass
class C(A):
# def test(self):
# print('from C')
pass
class D(B):
# def test(self):
# print('from D')
pass
class E(C):
# def test(self):
# print('from E')
pass
class F(D, E):
# def test(self):
# print('from F')
pass
f1 = F()
f1.test()
super()和mro列表
class People():
school = 'SH'
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
class Teacher(People):
def __init__(self, name, age, gender, level):
self.level = level
super().__init__(name, age, gender) # super的使用
# mro列表练习1
class A:
def test(self):
print('from A.test')
super().test()
class B:
def test(self):
print('from B')
class C(A, B):
pass
c = C()
c.test()
# mro列表练习2
class B:
def test(self):
print('B---->test')
def aaa(self):
print('B---->aaa')
class A:
def test(self):
print('A---->test')
super().aaa()
class C(A, B):
def aaa(self):
print('C----->aaa')
c = A()
# c.test() # 打印结果:
print(A.mro())
抽象类
抽象类是一个特殊的类,它的特殊之处在于只能被继承,不能被实例化
为什么要有抽象类
如果说类是从一堆对象中抽取相同的内容而来的,那么抽象类就是从一堆类中抽取相同的内容而来的,内容包括数据属性和函数属性
#一切皆文件
import abc #利用abc模块实现抽象类
class All_file(metaclass=abc.ABCMeta):
all_type='file'
@abc.abstractmethod #定义抽象方法,无需实现功能
def read(self):
'子类必须定义读功能'
pass
@abc.abstractmethod #定义抽象方法,无需实现功能
def write(self):
'子类必须定义写功能'
pass
# class Txt(All_file):
# pass
#
# t1=Txt() #报错,子类没有定义抽象方法
class Txt(All_file): #子类继承抽象类,但是必须定义read和write方法
def read(self):
print('文本数据的读取方法')
def write(self):
print('文本数据的读取方法')
class Sata(All_file): #子类继承抽象类,但是必须定义read和write方法
def read(self):
print('硬盘数据的读取方法')
def write(self):
print('硬盘数据的读取方法')
class Process(All_file): #子类继承抽象类,但是必须定义read和write方法
def read(self):
print('进程数据的读取方法')
def write(self):
print('进程数据的读取方法')
txt1=Txt()
sata1=Sata()
process1=Process()
#这样大家都是被归一化了,也就是一切皆文件的思想
txt1.read()
sata1.write()
process1.read()
print(txt1.all_type)
print(sata1.all_type)
print(process1.all_type)
面向对象的三大特征之二:多态
多态指的是一类事物有多种形态
比如动物有多种形态:人,狗,猪
import abc
class Animal(metaclass=abc.ABCMeta): #同一类事物:动物
@abc.abstractmethod
def talk(self):
pass
class People(Animal): #动物的形态之一:人
def talk(self):
print('say hello')
class Dog(Animal): #动物的形态之二:狗
def talk(self):
print('say wangwang')
class Pig(Animal): #动物的形态之三:猪
def talk(self):
print('say aoao')
多态性
多态性是指在不考虑实例类型的情况下使用实例
在面向对象方法中一般是这样表述多态性:
向不同的对象发送同一条消息(!!!obj.func():是调用了obj的方法func,又称为向obj发送了一条消息func),不同的对象在接收时会产生不同的行为(即方法)。
也就是说,每个对象可以用自己的方式去响应共同的消息。所谓消息,就是调用函数,不同的行为就是指不同的实现,即执行不同的函数。
比如:老师.下课铃响了(),学生.下课铃响了(),老师执行的是下班操作,学生执行的是放学操作,虽然二者消息一样,但是执行的效果不同
peo=People()
dog=Dog()
pig=Pig()
#peo、dog、pig都是动物,只要是动物肯定有talk方法
#于是我们可以不用考虑它们三者的具体是什么类型,而直接使用
peo.talk()
dog.talk()
pig.talk()
#更进一步,我们可以定义一个统一的接口来使用
def func(obj):
obj.talk()

浙公网安备 33010602011771号