Python基础笔记7-简说继承-面向对象part2


温馨提示:手机观看时,代码块可以左右滑动

继承

  • 如果在子类中定义了__init__()方法,父类的__init__() 不会被自动调用,需要在子类中显示调用

  • 子类中调用父类方法时,以父类类名为前缀,并以self做为第一个参数.区别于在类中调用普通函数是不需要带上self参数

  • Python总是首先在子类中查找对应方法,如果子类中没有找到,才会找到父类中逐个查找

    单继承

  • 当我们定义了一个类A,还需要建立一个B,但是A和B两者属性大致相同。那么可以利用继承的方法,让B继承A的属性,而不是重头开始写个B。

class University():
    def __init__(self):
        self.level = '双一流大学'
        print("University:yes")
    def getLevel(self):
        print('University:self level is:',self.level)

class College(University):
    pass

a = University()   # 返回University:yes
a.getLevel()       # 返回University:self level is: 双一流大学
b = College()      # 返回University:yes
b.getLevel()       # 返回University:self level is: 双一流大学

#在这个例子中,子类College默认没有__init__属性时,会直接继承父类University的__init__
#如果子类中有定义__init__,子类必须显示地在__init__()函数中再次调用父类的__init__()函数。

继承与抽象

  • 继承是基于抽象的过程
  • 以下是大学里建立教职工与学员信息表的代码,如下:
class Teachers():   #建立教职工的类
    def __init__(self,name,age,tea_id):
        self.name = name
        self.age = age
        self.tea_id = tea_id
    def say_hello(self):
        print('My name is {},{} years old'.format(self.name,self.age))
        print('I am a teacher,my id is {}'.format(self.tea_id))

class Students():   #建立学生的类
    def __init__(self,name,age,stu_id):
        self.name = name
        self.age = age
        self.stu_id = stu_id
    def say_hello(self):
        print('My name is {},{} years old'.format(self.name,self.age))
        print('I am a student,my id is {}'.format(self.stu_id))
  • Students和Teachers类都有共同的属性name和age,并且say_hello方式相同,可以对他们进行抽象,归类为People,建立派生类Sudents和Teachers。代码如下:
class People:
    def __init__(self,name,age,id):
        self.name = name
        self.age = age
        self.id = id
    def say_hello(self):
        print('My name is %s,%s years old'%(self.name,self.age))
        print('My id is %s'%self.id)

class Students(People):
    pass

class Teachers(People):
    pass

p = Students('amy','18','o090909')
q = Teachers('john','22','i090979')

p.say_hello()
#返回结果如下:
# My name is amy,18 years old
# My id is o090909

q.say_hello()
#返回结果如下:
# My name is john,22 years old
# My id is i090979

覆盖

  • 现在需要解决的就是要区分出是老师还是学生
  • 思路:
    • 覆盖People中的say_hello方法
    • Students调用say_hello方法时多输出一句"I am a student",同理Teachers类多输出一句"i am a teacher"
  • 调整后代码如下:
class People:
    def __init__(self,name,age,id):
        self.name = name
        self.age = age
        self.id = id
    def say_hello(self):
        print('My name is %s,%s years old'%(self.name,self.age))
        print('My id is %s'%self.id)

class Students(People):
    def say_hello(self):
        People.say_hello(self) #调用父类方法
        print('I am a student') 

class Teachers(People):
    def say_hello(self):
        People.say_hello(self) #调用父类方法
        print('I am a teacher')
        
p = Students('amy','18','o090909')
q = Teachers('john','22','i090979')

p.say_hello()
#返回结果如下:
# My name is amy,18 years old
# My id is o090909
# I am a student

q.say_hello()
#返回结果如下:
# My name is john,22 years old
# My id is i090979
# I am a teacher

多重继承

  • 子类可以继承多个父类
class Father1():
    father1_data = 'this is father_1'
    def father_1(self):
        return 'this is father_1 def'

class Father2():
    father2_data = 'this is father_2'
    def father_2(self):
        return 'this is father_2 def'

class Son(Father1,Father2):#子类Son同时继承Father1和Fahter2类
    def son_def(self):
        return 'this is son def'

s = Son()
print(s.father1_data) # 返回this is father_1
print(s.father2_data) # 返回this is father_2
s.father_1()  # 返回'this is father_1 def'
s.father_2()  # 返回'this is father_2 def'

上一篇:Python基础笔记6-那些被遗忘又好用的函数
下一篇:敬请期待

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容