面向对象的继承

面向对象三大特征

继承

# 面向对象三大特征: 封装 继承 多态
什么是继承?
  继承就是新建类的一种方式,新建的类我们称为 子类 或者叫派生类,被继承的类我们称为父类或者基类
  子类可以使用父类中的属性或者方法
  
为什么要用继承?
	类解决了对象与对象之间的代码冗余问题
  继承解决的是类与类之间的代码冗余问题
  
如何使用继承?
新式类:继承了object类的子子孙孙类都是新式类
经典类:没有继承object类的子子孙孙类都是经典类
# 新式类再经典类只有再python2中区分
#pzcharm 都是新式类

类的继承

# 父类
class Parent1:
    pass


# 父类
class Parent2:
    pass


# (类名) 括号中添加类名,那么这个函数将成为括号内的子类,反之,括号类的类名将是父类
# 子类
class Sub1(Parent1):
    pass

# 子类
class Sub2(Parent1, Parent2):
    pass


# __bases__ 查看Sub1所有继承的类
print(Sub1.__bases__)  # (<class '__main__.Parent1'>,)
print(Sub2.__bases__)  # (<class '__main__.Parent1'>, <class '__main__.Parent2'>)

# 使用__bases__查看父类
print(Parent1.__bases__)  # (<class 'object'>,)
print(Parent2.__bases__)  # (<class 'object'>,)
"""
	说明父类默认继承object类,这是一个内置的类
	变现形式class Parent1(object)
"""

类的继承实战举例1)


# 学生类
class Student():
    school = 'SH'
    
    def __init__(self,name,age,gender, course=None):
        if course is None:
            course = []
        self.name = name
        self.age = age
        self.gender = gender
        self.course = []
        
    def choose_course(self,course):
        self.course.append(course)
        print('%s 选课成功 %s' % (self.name, self.course))
    
# 教师类
class Teacher():
    school = 'SH'
    def __init__(self,name,age,gender, level):
        self.name = name
        self.age = age
        self.gender = gender
        self.level = level
        
    def score(self,stu_obj, score):
        stu_obj.score = score
        print('%s 给 %s 打了 %s 分' % (self.name, stu_obj.name, score))
        
# 学生类和教师类中有 school name age gender都需要使用的属性,那么,就可以创建一个父类,将二者都需要使用的放到公共类中
    

类的继承实战举例2)

# 父类,学生和教师都需要使用的类
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.course = course

    def choose_course(self, course):
        self.course.append(course)
        print('%s 选课成功 %s' % (self.name, self.course))

# 教师类
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))


s1 = Student('junjie', 18, 'nan', 'python')
print(s1.course)

t1 = Teacher('junjie', 19, 'nv', 5)
print(t1.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()
# Foo.f2,Bar.f1

# 练习
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()
# Foo.f2,  Foo.f1
# 现在Bar的名称空间中寻找f2(),没有,由于Bar是Foo的子类找到f2输出Foo.f2,self谁调用就是谁,接着打印__f1的内容

Super()和mro()

class People():
    school = 'SH'

    def __init__(self, name, age, gender):
        self.name = name
        self.age = age
        self.gender = gender


class gogo():
    wazi = 'heisi'

    def __init__(self):
        print('我的')


# 学生类
class Student(People,gogo):

    def __init__(self, name, age, gender,course=None):
        if course is None:
            course = []
   # 使用super()会得到一个特殊的对象,该对象专门用来引用父类的属性,且严格按照MRO规定的顺序先后查找
        super(Student, self).__init__(name, age, gender)
        self.course = course
        # gogo.__init__(self)

    def get_go(self):
        print('我%s 今年%s %s %s %s' % (self.name, self.age,self.gender,self.wazi,self.school))


s = Student('junjie',18,'喜欢')
print(s.wazi) # heisi
s.get_go() # 我junjie 今年18 喜欢 heisi SH
print(Student.mro()) # [<class '__main__.Student'>, <class '__main__.People'>, <class '__main__.gogo'>, <class 'object'>]

溜了溜了

posted @ 2021-12-06 21:36  谢俊杰  阅读(41)  评论(0编辑  收藏  举报