Python classes abstract classes and interfaces

A, abstract classes and interfaces classes

Inherit two purposes:

A: The method of the base class inherits, and make their own changes or extensions (code reuse)  

II: declare a subclass is compatible with a particular base class that defines an interface class Interface, Interface class defines the interface name (that is, the function name) and did not implement the interface functions, subclass inherits the interface class and implement interface Features.

In practice, it inherited first meaning is not very significant, and often harmful. Because it enables the base class and subclasses strong coupling occurs.

Inherited second meaning is very important. It is also called the "interface inheritance."
Interface inheritance is essentially required "to make a good abstraction, which provides a compatible interface that allows external callers do not care about the details, can achieve non-discriminatory treatment of all objects of a particular interface" - in this programming called normalization.

1. make good abstract class, 2. provisions compatible interface 3. The caller may not care about the specific implementation details, can achieve equally deal with all objects of a particular interface.

Copy the code
# Make a good abstract 
class Payment (Object): 
    # provides a compatible interface 
    DEF the Pay (Self): 
        Pass 

# micro-channel pay 
class WeChatPay (Object): 
    DEF the Pay (Self, Money): 
        Print ( 'micro-channel paid% S '% Money) 

# Alipay 
class AliPay (Object): 
    DEF the pay (Self, Money): 
        Print (' Alipay payment of% S '% Money) 

# Apple pay 
class ApplePay (Object): 
    DEF the pay (Self, Money) : 
        Print ( 'Apple paid S%'% Money) 




DEF the pay (obj, Money): 
    obj.pay (Money) 

Weixin = WeChatPay () 
alipay = AliPay () 
applepay = ApplePay () 

# caller without caring about the specific implementation details , treatment may be equally achieved all the objects of a particular interface 
Pay (Weixin, 100)  
Pay (alipay, 200 is)
Pay (applepay, 300)
Copy the code

Normalized so that high-level external users can not indiscriminately process all interfaces compatible set of objects - as if the concept of pan-file linux, like, everything can be a file handle, it does not have to be concerned about memory, disk, network or screen (of course, the underlying designers, of course, can be distinguished "character device" and "block device", and then make targeted design: detail to what extent, depending on demand).

1
2
依赖倒置原则:
高层模块不应该依赖低层模块,二者都应该依赖其抽象;抽象不应该应该依赖细节;细节应该依赖抽象。换言之,要针对接口编程,而不是针对实现编程

Second, the abstract class

What is an abstract class

    Like with java, python also has the concept of an abstract class, but also need the aid of module implementation, an abstract class is a special class, it special is that can only be inherited, can not be instantiated

Why should an abstract class

    If the class is derived from a pile of objects extracted from the same content, the abstract class it from the pile of the class extracted from the same content, including data attributes and function attributes.

  For example, we have bananas category, Apple's class, there are peaches of classes, extracting the same content is the fruit of this abstract class from these classes, when you eat fruit, or eat a particular bananas, either eat a specific peach. . . . . . You can never eat something called fruit.

    See from the design point of view, if the class is an abstraction from the real object comes, then the abstract class is a class-based abstract come.

  From the implementation point of view, is different from the abstract classes in that general category: abstract class abstract method, the class can not be instantiated, can be inherited, and subclasses must implement the abstract methods. This interface is somewhat similar, but in fact is different, is about to reveal the answer

An abstract class in python

Copy the code
# Everything is a file 
import abc # abc module implements the use of abstract class 

class All_file (the metaclass that = abc.ABCMeta): 
    all_type = 'File' 
    @ abc.abstractmethod # define abstract methods without having to realize the function 
    def read (self): 
        sub-class' must read function defined ' 
        Pass 

    @ # abc.abstractmethod abstract methods defined, without implementing functions 
    DEF write (Self): 
        ' write function must define the subclass' 
        Pass 

# class the Txt (All_file): 
# Pass 
# 
# T1 = the Txt () error # , sub-class does not define abstract methods 

class Txt (All_file): # subclass inherits the abstract classes, but must define the read and write methods 
    DEF read (Self): 
        Print ( 'text data reading method') 

    DEF write (Self): 
        print ( 'text data reading method') 

class Sata (All_file): # subclass inherits the abstract classes, but must define the read and write methods 
    def read (self):
        print ( 'hard disk data reading method') 

    DEF write (Self): 
        print ( 'hard disk data reading method') 

class Process (All_file): # subclass inherits the abstract classes, but must define the read and write methods 
    DEF read (Self): 
        ( 'reading method process data') Print 

    DEF the Write (Self): 
        ( 'reading method process data') Print 

wenbenwenjian = Txt () 

yingpanwenjian = Sata () 

jinchengwenjian = process () 

# so we are normalized, that is, thinking everything is a file of 
wenbenwenjian.read () 
yingpanwenjian.write () 
jinchengwenjian.read () 

Print (wenbenwenjian.all_type) 
Print (yingpanwenjian.all_type) 
Print (jinchengwenjian.all_type)
Copy the code

Three abstract classes and interfaces Abstract

Essence is the abstract class, referring to the similarity of a set of classes, including data attributes (e.g. all_type) properties and functions (e.g. read, write), and the interface only emphasized the similarity function attributes.

An abstract class is a class between a direct and an interface concept, along with some features classes and interfaces can be used to achieve normalization Design 

In python, there is no such thing as an interface class, if not by specialized modules defined interfaces, we should have some basic concepts.

1. multiple inheritance problem

In the process of inheriting an abstract class, we should try to avoid multiple inheritance;
and in succession interface, we encourage you to instead multiple-inheritance interfaces

Interface segregation principle: 
a dedicated interface plurality instead of single general interface. That client should not rely on those unwanted interfaces.

In the abstract class, we can make on the basis of some abstract methods to achieve;
and in the interface class, any method is just a specification, specific functional needs subclass implementation

Copy the code
1. multiple inheritance problems 
in the process of inheriting an abstract class, we should try to avoid multiple inheritance; 
and in succession interface, we encourage you to multiple inheritance instead of interfaces 


to achieve 2. The method 
in an abstract class, we can abstract the method of making the underlying implementation; 
in the interface class, any method is just a specification, the specific functional needs subclass implementation
Copy the code

Guess you like

Origin www.cnblogs.com/zixinyu/p/11308626.html