Object-oriented inheritance

inherit

What is inherited

Inheritance is a relationship between two objects is described, what is the relationship between what

For example McDull, Paige, are Zhu A pig just mane,

In the program, describes the inheritance relationships between classes and class

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

Why use inheritance:

Inherited party may direct one party has inherited some things

Its purpose is to reuse existing code has been improved reusability

How to use inheritance

grammar:

class class name (the name of the parent class): 
    the contents of the class 
    
# python in a subclass can inherit multiple parent classes at the same time

 

class A:
    def __init__(self):
        self.name = "hello"
        self.age = 10
    def say_hello(self):
        print("say_hello")

class B(A):
    pass

b = B()
print(b.name)
print(b.age)
b.say_hello()

abstract:

Not specific, unclear, vague, not read

The same portion of the plurality of process subclasses, extraction, to form a new class, a process also known as abstract

The proper use of inheritance:

1. The first abstract inheritance

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

 

class C:
    def __init__(self,name,age):
        self.name = name
        self.age = age

    def say_hello(self):
        print("hello")

class A(C):
    def say_hi(self):
        print("hi")
        
a = A("jason",23)
a.say_hello()
a.say_hi()


class B(C):
    pass

b= B("egon",12)
print(b.name)
print(b.age)

Property search order

class A:
    text = "haha"

class B(A):
    text = "heihei"
    pass

b = B()
b.text = "xixi"

print(b.text)

The object itself -> where the class -> find parent -> parent class parent -> Object

Derivation

When a subclass occurred with different content parent class, subclass it is called a derived class

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

So derived class refers to a subclass

cover

Also known as rewrite overrides

When the sub-category appear entirely consistent with the name of the parent class property or method

class MyList(list):
    def __init__(self,else_type):
        super().__init__()
        self.else_type = else_type

    def append(self,object):
        if type(object)  == self.else_type:
            super().append(object)
        else:
            print("请输入指定的类型")


my_list = MyList(int)

my_list.append(12)
my_list.append(12)
my_list.append(12)
my_list.append(12)
my_list.append(12)
my_list.append("asdasd")
print(my_list)

He stressed stressed:

When you inherit an existing class, and you override the parent class init method, you must call the parent class initialization method of the first line of the initialization method, passing in the required parameters parent class

combination

It is also a relationship, describing what is what is the relationship between two objects

For example, students have cell phones, game characters have certain equipment

An object as a property of another object, (both what what)

The purpose of the combination:

Also to reuse existing code

When to use inheritance: analysis of the relationship between two classes, in the end is not: What is the relationship between what

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

class A:
    def __init__(self,obj):
        self.obj = obj

class B:
    def __init__(self):
        pass

    def say_hi(self):
        print("say_hi")
b = B()

a = A(b)
a.obj.say_hi()

Diamond inheritance

First clear python support multiple inheritance

 

Added: new-style class and Classic

Python3 in any class are inherited directly or indirectly Object

New class, any explicit or implicit object inherits from class to the new class is called, all of the new class to python3

Classic, is neither a subclass of Object, occurs only in python2

 

When there is a diamond inheritance, the new class, the first depth, when faced with a common parent class on breadth

The new category is depth-first

 

 

Guess you like

Origin www.cnblogs.com/cherish937426/p/11247057.html