内容概要
-
面向对象的三大特征
1.封装
2.继承
3.多态
-
继承的属性查找顺序
1.单继承下的属性查找
2.多继承下的属性查找
- super()和mro列表
- 多态与多态性
继承
1.什么是继承
继承就是新建类的一种方式,新建的类我们称为子类或派生类,被继承的类我们称为父类或基类
子类可以使用父类中的属性或方法
2.为什么要用继承
类解决了对象与对象之间的代码冗余问题
继承解决了类与类之间的代码冗余问题
3.如何使用继承
新式类:继承了object类的子子孙孙类都是新式类
经典类:没有继承object类的子子孙孙类都是经典类
新式类和经典类只有在python2中有区分
类的继承
# 父类
class People():
school = 'zj'
def __init__(self, name, age, hobby):
self.name = name
self.age = age
self.hobby = hobby
# 学生类(子类)
class Student(People):
def __init__(self, name, age, hobby, course=None):
if course is None:
course = []
People.__init__(self, name, age, hobby)
self.courses = course
def choice_course(self, course):
self.courses.append(course)
print('学生%s选择%s课程' % (self.name, self.courses))
stu = Student('aaa', 18, 'music')
stu.choice_course('music')
# 教师类(子类)
class Teacher(People):
def __init__(self, name, age, hobby, ):
People.__init__(self, name, age, hobby)
def score(self, stu1, stu2, score):
print('教师%s给%s的%s课程打%s分' % (self.name, stu1, stu2, score))
tea = Teacher('sss', 27, 'run')
tea.score('aaa', 'music', 90)
单继承下属性查找
class Animal():
def dog(self):
print('is dog')
def cat(self):
print('is cat')
self.dog()
class Crawl(Animal):
def dog(self):
print('one dog')
obj = Crawl()
obj.cat()
练习
class Animal():
def __dog(self): # _Animal__dog
print('is dog')
def cat(self):
print('is cat')
self.__dog() # _Animal__dog
class Crawl(Animal):
def __dog(self): # _Crawl__dog
print('one dog')
obj = Crawl()
obj.cat()
多继承下的属性查找
新式类:按照广度优先查询
经典类:按照深度优先查询
class A():
def test(self):
print('from A')
class B(A):
def test(self):
print('from B')
class C(A):
def test(self):
print('from C')
class D(B):
def test(self):
print('from D')
class E(C):
def test(self):
print('from E')
class F(D, E):
def test(self):
print('from F')
obj = F()
obj.test()
super()和mro()列表
super()用法
# 父类
class People():
school = 'zj'
def __init__(self, name, age, hobby):
self.name = name
self.age = age
self.hobby = hobby
# 学生类(子类)
class Student(People):
def __init__(self, name, age, hobby, course=None):
if course is None:
course = []
# People.__init__(self, name, age, hobby)
# super(Student, self)返回一个特殊对象
# 他的使用遵循mro列表
super(Student, self).__init__(name, age, hobby)
self.courses = course
def choice_course(self, course):
self.courses.append(course)
print('学生%s选择%s课程' % (self.name, self.courses))
stu = Student('aaa', 18, 'music')
print(stu.name)
# 教师类(子类)
class Teacher(People):
def __init__(self, name, age, hobby, ):
# People.__init__(self, name, age, hobby)
super().__init__(name, age, hobby)
def score(self, stu1, stu2, score):
print('教师%s给%s的%s课程打%s分' % (self.name, stu1, stu2, score))
tea = Teacher('sss', 27, 'run')
print(tea.age)
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()
print(C.__mro__)
练习2
class A:
def test(self):
print('A---->test')
super().aaa()
class B:
def test(self):
print('B---->test')
def aaa(self):
print('B---->aaa')
class C(A, B):
def aaa(self):
print('C----->aaa')
c = C()
c.test()
print(C.__mro__)
练习3
class A:
def test(self):
print('A---->test')
super().aaa()
class B:
def test(self):
print('B---->test')
def aaa(self):
print('B---->aaa')
class C(A, B):
def aaa(self):
print('C----->aaa')
c = A()
print(A.__mro__)
c.test() # 报错
什么事多态
水:液态水,固态水,气态水
import abc
# 抽象类: 抽象类只能被继承,不能被实例化
class Animal(metaclass=abc.ABCMeta):
@abc.abstractmethod # 该方法已经是抽象方法了
def speak(self): pass
@abc.abstractmethod
def login(self): pass
class People(Animal):
def speak(self):
# print('嗷嗷嗷')
pass
def login(self):
pass
class Pig(Animal):
def speak(self):
print('哼哼哼')
class Dog(Animal):
def speak(self):
print('汪汪汪')
obj = People()
obj.speak()
# 多态练习
class Pig():
def speak(self):
print('哼哼哼')
class Dog():
def speak(self):
print('汪汪汪')
class Txt():
def speak(self):
print('Txt')
obj = People()
obj1 = Pig()
obj2 = Dog()
obj3 = Txt()
# 多态带来的特性:在不用考虑对象数据类型的情况下,直接调用对应的函数
def animal(animal):
return animal.speak()
animal(obj)
animal(obj1)
animal(obj2)
animal(obj3)
# 父类限制子类的行为
class Animal():
def speak(self):
raise Exception("必须实现speak方法")