继承 派生 super()经典类 新式类

'''
1什么是继承?
继承一种新建类的方式,在python中支持一个儿子继承多个爹
新建的类称为子类的或者派生类
父类有可以称为基类或者超类

子类会‘遗传’父类的属性

2 为什么要用继承
减少代码冗余

3 怎么用继承

'''

# class ParentClass1:
# pass
#
# class ParentClass2:
# pass
#
# class Subclass1(ParentClass1):
# pass
# class Subclass2(ParentClass1,ParentClass2):
# pass

# print(Subclass2.__bases__)
# (<class '__main__.ParentClass1'>, <class '__main__.ParentClass2'>)

# 在python2中有经典类与新式类之分
# 在python3中全部为新式类

'''
1 继承是类与类之间的关系,寻找这种关系需要先抽象再继承
'''
# class OldboyPeople:
# school='oldboy'
#
# def __init__(self,name,age,sex):
# self.name=name
# self.age=age
# self.sex=sex
#
# class OldboyTeacher(OldboyPeople):
# def change_score(self):
# print('teacher %s is changing score'%self.name)
#
#
# class Oldboystudent(OldboyPeople):
# def choose(self):
# print('student %s choose course'%self.name)

# tea1=OldboyPeople('egon',18,'male') ##OldboyPeople.__init__()
# stu1=OldboyPeople('alex',73,'female')

# print(tea1.name,tea1.age,tea1.sex)
# egon 18 male
# print(stu1.name)
# alex
#

# class OldboyPeople:
# school = 'oldboy'
#
# def __init__(self, name, age, sex):
# self.name = name
# self.age = age
# self.sex = sex
#
# def f1(self):
# print('爹的f1')
# class OldboyTeacher(OldboyPeople):
# def change_score(self):
# print('teacher %s is changing score' %self.name)

# tea1 = OldboyTeacher('egon', 18, 'male')
# print(tea1.__dict__) #{'name': 'egon', 'age': 18, 'sex': 'male'}
#
# print(tea1.name) #egon
#
# print(tea1.school) #oldboy
#
# tea1.change_score() #teacher egon is changing score
# tea1.f1() #爹的f1


# class Foo:
# def f1(self):
# print('Foo.f1')
#
# def f2(self): #self=obj
# print(self) #<__main__.Bar object at 0x0000000002173898>
#
# print('Foo.f2')
# self.f1() #obj.f1()
#
# class Bar(Foo):
# def f1(self):
# print('Bar.f1')
#
# obj =Bar()
# print(obj.__dict__) #{}
#
# obj.f2()

# Foo.f2
# Bar.f1


'''
派生:子类定义自己新的属性,如果与父类同名,以子类自己的为准
'''
# class OldboyPeople:
# school = 'oldboy'
#
# def __init__(self, name, age, sex):
# self.name = name
# self.age = age
# self.sex = sex
#
# def f1(self):
# print('爹的f1')
# class OldboyTeacher(OldboyPeople):
# def change_score(self):
# print('teacher %s is changing score' %self.name)
#
# def f1(self):
# print('儿子的f1')
#
# tea1 = OldboyTeacher('egon', 18, 'male')
#
# tea1.f1() #儿子的f1 子类有的用子类的 没有网上查
#
#

# class OldboyPeople:
# school = 'oldboy'
#
# def __init__(self, name, age, sex):
# self.name = name
# self.age = age
# self.sex = sex
#
# def f1(self):
# print('爹的f1')
#
# class OldboyTeacher(OldboyPeople):
# def __init__(self,name,age,sex,level,salary):
# self.name=name
# self.age=age
# self.sex=sex
#
# self.level=level
# self.salary=salary
#
# def change_score(self):
# print('teacher %s is changing score' %self.name)
#
# def f1(self):
# print('儿子的f1')
#
# tea1 = OldboyTeacher('egon', 18, 'male',9,3.1)
# print(tea1.name,tea1.age,tea1.sex,tea1.level,tea1.salary)

'''
在子类派生出的新方法中重用父类的功能
方式一:指名道姓地调用(其实与继承没有什么关系的)
OldboyPeople.__init__(self,name, age, sex)
'''

#
# class OldboyPeople:
# school = 'oldboy'
#
# def __init__(self, name, age, sex):
# self.name = name
# self.age = age
# self.sex = sex
#
# def tell_info(self):
# print("""
# ===========个人信息==========
# 姓名:%s
# 年龄:%s
# 性别:%s
# """ %(self.name,self.age,self.sex))


# class OldboyTeacher(OldboyPeople):
# # tea1,'egon', 18, 'male', 9, 3.1
# def __init__(self, name, age, sex, level, salary):
# # self.name = name
# # self.age = age
# # self.sex = sex tea1 先调用OldboyTeacher __init__ self 已经把自己传入了
# OldboyPeople.__init__(self,name, age, sex) #在类中调用类自己的方法(不是嵌套) 类自己调方法就是当做普通的函数调用 需要几个值
# #就传入几个
#
# self.level = level
# self.salary = salary
#
# def tell_info(self):
# OldboyPeople.tell_info(self) #指名道姓的调用类里面的方法
# print("""
# 等级:%s
# 薪资:%s
# """ %(self.level,self.salary))
#
# tea1 = OldboyTeacher('egon', 18, 'male', 9, 3.1)
# # print(tea1.name, tea1.age, tea1.sex, tea1.level, tea1.salary)
#
#
# # tea1.tell_info()

'''
方法二:super()调用(严格以来依赖于继承)
super()的返回值是一个特殊的对象,该对象专门用来调用父类中的属性
在python2中 需要super(当前 自己的类名,self)
python3中super()可以不用传参数
'''
# class OldboyPeople:
# school = 'oldboy'
#
# def __init__(self, name, age, sex):
# self.name = name
# self.age = age
# self.sex = sex
#
# def tell_info(self):
# print("""
# ===========个人信息==========
# 姓名:%s
# 年龄:%s
# 性别:%s
# """ %(self.name,self.age,self.sex))
#
# class OldboyTeacher(OldboyPeople):
# # tea1,'egon', 18, 'male', 9, 3.1
# def __init__(self, name, age, sex, level, salary):
# # OldboyPeople.__init__(self,name, age, sex)
# super(OldboyTeacher,self).__init__(name,age,sex)
#
# self.level = level
# self.salary = salary
#
# def tell_info(self):
# # OldboyPeople.tell_info(self)
# super().tell_info()
# print("""
# 等级:%s
# 薪资:%s
# """ %(self.level,self.salary))
#
# tea1 = OldboyTeacher('egon', 18, 'male', 9, 3.1)
# # print(tea1.name, tea1.age, tea1.sex, tea1.level, tea1.salary)
# tea1.tell_info()

'''
1 新式类:
继承object 的类,以及该类的子类,都是新式类

在python3中,如果一个类没有指定继承的父类,默认就继承object
所以说python3中所有的类都是新式类

2 经典类(只有在python2才区分经典类与新式类):
没有继承object的类,以及该类的子类,都是经典类

'''
# class Foo(object):
# pass
#
# class Bar(Foo):
# pass
#
# print(Foo.__bases__)
# print(Bar.__bases__)
'''
1. 什么是继承
继承是一种新建类的方式,新建的类称之为子类/派生类,被继承的类称之为父类/基类/超类

继承有3个特点:
1. 子类可以遗传/重用父类的属性(解决类与类之间代码冗余的问题)
2. 在python中一个子类可以同时继承多个父类
3. 在继承的背景下,类分为两种:新式类,经典类
新式类:但凡继承object类的子类,以及该子类的子子孙孙都是新式类
在python3中一个类即便是没有显式地继承任何类,默认就继承object类,
即在python3中所有类都是新式类
经典类:没有继承object类的子类,以及该子类的子子孙孙都是经典类
强调:只有在python2中才有经典类
在python2中如果一个类没有显式地继承任何类,并不会自动继承object类

2. 为何要用继承:减少类与类之间的代码冗余

3. 如何继承
class Foo:
pass
class Bar(Foo):
pass

#obj=Bar()
#obj.x

Bar.x

4. 属性查找
在没有出现菱形继承的情况下,属性的查找是按照从左往右的顺序一个分支一个分支的找下去
在出现菱形继承(一个子类继承的多个分支最终汇聚到一个非object类)的情况下,
新式类:广度优先查找,按照从左往右的顺序一个分支一个分支的找下去,在最后一个分支才去查找顶级类
经典类:深度优先查找,按照从左往右的顺序一个分支一个分支的找下去,在第一个分支就查找顶级类


5. 派生
在子类中定义自己的属性,如果与父类的属性重名,那以自己的为准.

在子类派生的新方法中重用父类的功能:
方式一:指名道姓地调用某一个类的函数
特点:
1. 与继承无关
2. 没有自动传值的效果

方式二:super()得到一个特殊的对象,该对象专门用来引用父类的属性
特点:
1. 严格依赖继承,完全参照mro
2. 有自动传值的效果
'''

猜你喜欢

转载自www.cnblogs.com/lakei/p/10738746.html