Three characteristics of object-oriented - inheritance

First, what is inherited

Inheritance is a relationship between two objects is described, what is what relationship. What is the association between the two classes, you can use the same property or method.

E.g. inherited a b, a b can be used directly in existing methods and properties.

called a subclass, b is called the parent class, also called a base class.

Second, why should use inheritance

Inherited the party can be used directly inherited the party has some things, its purpose is to reuse code already written, improve reusability.

Third, how to use inheritance

grammar:

class    class name (the name of the parent class): 
           content class 

# in python in a subclass can inherit more than one parent class behind the diamond inheritance has summarize

Fourth, the abstract concept

This does not refer to abstract, not concrete description of the concepts we usually say ambiguous.

This is part of a plurality of the same subclass, extraction, to form a new class, a process also known as the abstract process.

Correct use inherited methods:

1. The first abstract and then inherit

2. inherit an existing class, extend or modify the original function

Fifth, property search order

The object is located in your own class >>> >>> >>> object to find parent parent parent class >>>

Six derivative (this is just a point to remember knowledge, we will usually write, just do not know what was the name)

When there is a parent class with different contents of a subclass, this subclass it is called a derived class.

Usually sub-class will write some new code, and the parent can not be exactly the same, that is usually derived class.

So derived class refers to a subclass

Seven, to cover (only if this is a knowledge to understand)

It may be referred to as a subclass overrides rewritten appears entirely consistent with the method of the parent class name attribute or

Inheritance list, rewrite append method to determine only add int data type, can not fail to add

Emphasize! ! ! (Best to do so)

When you inherit a class time, and you override the ancestor __init__ method, you must call the parent class initialization method in the first line of the initialization method, passing parameters parent needs.

class Mylist (List):
     DEF  the __init__ (Self, type): 
        . Super () the __init__ ()     # the __init__ method inherited from the parent class 
        self.type type =     # example of a transmission parameter 

    DEF the append (Self, NUM):
         IF of the type (NUM) == self.type: 
            . Super () the append (NUM) 
            Print ( ' added successfully ' )
         the else :
             Print ( ' failed to add, you pass parameter data types are not ' ) 


the p- = Mylist (int) # set values pass type 
p.append ( 2) pass a value # 2

Eight combinations (this is also very important, and usefulness of the inherited almost)

Is a relationship between two objects describe what is what relationship      , for example, students have cell phones, game characters have certain equipment

An object as an attribute of another object (an instance of that object as the parameter passed to another object)

Objective combination:  also to reuse existing code

When to use inheritance: analyze the relationship between two classes, if it belongs to a class, then it can be inherited.

For example: There are animal eating and sleeping, eating and sleeping humans have, but we usually do not inherit eating and sleeping animals by humans, can not be justified logically, but if you want to do with it, or can be used, but that humans inherited a bit strange animals. There can write a parent class eating and sleeping, human and animal classes inherit from the other side.

When to use a combination of: if there is no significant relationship between the two classes, do not belong to the same.

Further, compared hierarchical composition, the lower the degree of coupling

Two classes: a telephone, a student. Now demand is the students also want to use the phone inside call_phone class method, use inheritance here if logic does not make sense, can only be used in combination

class Phone:
     DEF  the __init__ (Self, kind,. price): 
        self.kind = kind 
        self.price = . price
     DEF CALL_PHONE (Self):
         Print ( ' Call ' ) 

class Student:
     DEF  the __init__ (Self, name, Age, Phone ) : # here remember to add a parameter 
        self.name = name 
        self.age = Age
         self.phone = Phone # here, too, plus 

    DEF show_info (Self):
         Print ( "name: Age% S:% S " % (the self.name, self.age)) 

phone = Phone ( ' Apple ' , 10000 ) 
S1 = Student ( ' Rose ' , 20 is , phone ) # The phone objects passed as parameters s1 objects in
 s1.phone.call_phone ()    

Nine, subclass the parent class content access

grammar

Mode. 1: 
Super (current class name, self) you want to adjust the properties or methods of the parent class. 
Manner 2: 
Super () you want to adjust the properties or methods of the parent class. 
Embodiment 3: 
the name that you want to transfer the parent. class property or method (Self)   
# manner regardless of the inheritance 3
class Parent:
    text = "abc"

    def say_something(self):
        print("anything")

class Sub(Parent):

    def show_info(self):
        print(super().text)
        super().say_something()

sub = Sub()
sub.show_info()

Ten, diamond inheritance

Any python3 classes are inherited directly or indirectly Object 
new class: any explicit or implicit object class that inherits from class to the new call, python3 in all new categories
Classic: neither a subclass of Object, python2 appear only in when there is a diamond inheritance when: python3 If the new class, the first depth-first, when faced with a common parent class to find python2 classic breadth, depth priority is to keep

 

Guess you like

Origin www.cnblogs.com/wangcuican/p/11247183.html