day21-- inheritance - derived

inherit:

What is inherited?

Inheritance is a new type of way, the new class is called a word class or a derived class, called a parent class inherits a base class or superclass.

In python, subclasses inherit a plurality of parent classes.

In other languages, a subclass can only inherit from a parent class.

Inherited role:

Solving redundant code.

How inheritance?

1. make sure who is a subclass, who is the parent class.

2. When defining classes subclass, class name (the name of the parent class).

# 父类
class Father1:
    x = 1
    pass

class Father2:
    pass

#子类
class Sub(Father1, Father2):
    pass
#子类.__bases__查看父类
print(Sub.__bases__)
print(Sub.x)

How to find inheritance:

1. Verify who is a subclass

2. Determine who is the parent class

- between the extracted objects like parts summarized class.

- extracting portion between similar classes summarized parent class.

#代码冗余:
#老师类
class OldboyTeacher:
    school = 'oldboy'
    country = 'China'

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

    def change_score(self):
        print(f'老师{self.name}正在修改分数...')

#学生类
class OldboyStudent:
    school = 'oldboy'
    country = 'China'

    def __init__(self, name, age, sex):
        self.name = name
        self.age = age
        self.sex = sex
        #学生选课程
    def choose_course(self):
        print(f'学生{self.name}正在选择课程...')

#学生类
stu1 = OldboyStudent('AA', 19, 'female')
print(stu1.school, stu1.name, stu1.age, stu1.sex) #oldboy AA 19 female

#老师
tea1 = OldboyTeacher('tank', 18, 'male')
print(tea1.school, tea1.name, tea1.age, tea1.sex) # oldboy tank 18 male

# 解决代码冗余问题
#老男孩人类
class OldboyPeople:  #我们经过分析可以得到上面学生类和老师类共同拥有                       的特征,我们定义老男孩人类的父类。
    school = 'oldboy'
    country = 'China'

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

#老师类:
class OldboyTeacher(OldboyPeople):  #我们下面可以直接继承父类
    # 老师修改分数
    def change_score(self):
        print(f'老师{self.name}正在修改分数...')

# 学生类
class OldboyStudent(OldboyPeople):   # #我们下面可以直接继承父类
    #学生选课程
    def choose_course(self):
        print(f'学生{self.name}在选择课程...')

stu1 = OldboyStudent('AA', 39, 'female')
print(stu1.school, stu1.name, stu1.age, stu1.sex)#oldboy AA 39 female
tea1 = OldboyTeacher('大脸', 75, 'female')
print(tea1.school, tea1.name, tea1.age, tea1.sex)#oldboy 大脸 75 female

Find the order of object properties inherited background

Note: The sequence program is executed from top to bottom, it must be defined in the parent class above the subclass.

- In the context of inheritance, lookup order object properties:

1. The first look in the object's own namespace

2. The object is not to find the namespace subclass.

3. subclass does not look from the parent class namespace, if the parent is not, the error!

Derivation

It refers to the subclass inherits the properties and methods of the parent class, and derive their own unique attributes and methods.

If the same method in the subclass and the parent class, by priority subclass.

#父类:
class Foo:
    def f1(self):
        print('from Foo.f1...')

    def f2(self):
        print('from Foo.f2...')
        self.f1()

#子类
class Bar(Foo):
    def f1(self):
        print('from Bar.f1...')
    def func(self):
        print('from Bar.func...')

# bar_obj = Bar()
# bar_obj.f1() # from Bar.f1...
# bar_obj.func()  # from Bar.func...
# bar_obj.f2()  # from Foo.f2...

bar_obj = Bar()
bar_obj.f2() #from Foo.f2...
                #from Bar.f1...  #因为子类和父类中的方法名一样,优先使用子类名,

Subclass inherits by both the parent class attributes and methods of the parent class

Subclass inherits the parent class, derive their properties and methods, and reuse the properties and methods of the parent class.

Question: subclass overrides the parent class _ _ init _ _ lead to code more redundancy

To solve the problem: Sub-class reuse property of the parent class, and derive new property
- in two ways:
- 1. direct reference to the parent class _ _ init _ _ its mass participation, and add attributes subclasses.
--2. Pointed to by the properties of the parent class Super.
--super () is a special class, call the super to get a object that points to the parent class namespace.

注意: 使用哪一种都可以,但不能两种方式混合使用。

#解决方法
#方式一
class OldboyPeople:
    school = 'oldboy'
    def __init__(self, name, age, sex):
        self.name = name
        self.age = age
        self.sex = sex

class OldboyTeacher(OldboyPeople):

    def __init__(self, name, age, sex, sal):
        # self.name = name
        # self.age = age
        # self.sex = sex
        # #类调用类内部的__init__,值时一个普通函数
        OldboyPeople.__init__(self, name, age, sex)
        self.sal = sal

    def change_score(self):
        print(f'老师{self.name}修改分数...')

class OldboyStudent(OldboyPeople):

    def __init__(self, name, age, sex, girl):
        OldboyPeople.__init__(self, name, age, sex)
        self.girl = girl
    def choose_course(self):
        print(f'学生{self.name}选择课程...')

tea1 = OldboyTeacher('tank', 17, 'male', 15000000)
print(tea1.name, tea1.age, tea1.sex, tea1.sal)

stu1 = OldboyStudent('姚玉鑫', 28, 'male', '凤姐')
print(stu1.name, stu1.age, stu1.sex, stu1.girl)

#方法二
class OldboyPeople:
    school = 'oldboy'
    def __init__(self, name, age, sex):
        self.name = name
        self.age = age
        self.sex = sex

class OldboyTeacher(OldboyPeople):
    def __init__(self, name, age, sex, sal):
        super().__init__(name, age, sex)
        self.sal = sal

    def change_score(self):
        print(f'老师{self.name}修改分数...')

class OldboyStudent(OldboyPeople):
    def __init__(self, name, age, sex, girl):
        super().__init__(name, age, sex)
        self.girl = girl

    def choose_course(self):
        print(f'学生{self.name}选择课程...')

tea1 = OldboyTeacher('tank', 17, 'male', 15000000)
print(tea1.name, tea1.age, tea1.sex, tea1.sal)


stu1 = OldboyStudent('姚玉鑫', 28, 'male', '凤姐')
print(stu1.name, stu1.age, stu1.sex, stu1.girl)
Classic and new-style class (understand)
  • The new categories:

    - 1. Those who inherit the object class or classes are descendants of the new class.

    - 2. In python3 in all classes inherit the default object.

  • Classic:

    - 1. In the python2 will have a sub-category of Classic and modern.

    - 2. In python2, the object of the class who did not inherit, is a classic class.

supre strictly follow the order of succession mro

Mro call returns a sequence succession: (understanding of knowledge)

Strictly follow the order of succession super mro inherited sequence.

Find new class provides a built-in method to find order in python3 in mro ():. Will inherit the current class of relational out.

class Father1:
    x = 10
    pass
class Father2:
    x = 20
    pass
#多继承的情路况下:从左到右
class Sub(Father1, Father2):
    def __init__(self):
        print(super().__delattr__)

print(Sub.mro())
obj = Sub()
print(object)

Multiple inheritance case caused "diamond inheritance"

Find mro order:

The new categories:

Breadth-First

Classic:

Depth-first

# 了解:
# 新式类:
class A(object):
    # def test(self):
    #     print('from A')
    pass

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

# F-->D-->B-->E-->C-->A-->object
# print(F.mro())
obj = F()
obj.test()

Guess you like

Origin www.cnblogs.com/lishuangjian/p/11938302.html