8.python Face Object Part.4 (the thought design of interface inheritance, this kind of thing is also called abstract class)

Source: Internet
Author: User
Tags define abstract

Interface inheritance is also called the normalization design of the program, and this kind of thing is called abstract class.

So what is abstract class?

Abstract class This kind of thing, can only be inherited, can not be instantiated.

If a class is extracted from a bunch of objects, an abstract class is extracted from a bunch of classes.

Simply put, the biggest difference between abstract class and ordinary class is that abstract class methods are tried abstract, there is no way to achieve the specific function, this kind of abstract class can not generate objects, only inherit, but, inherit the subclass of this abstract class, must be to implement abstract class abstraction out of the way! This concept is very much like an interface, so I understand it as an interface inheritance.

You can refer to the following example for the definition of abstract classes.

Before implementing the abstract class definition, Python provided me with a module called "ABC", which can be used to define abstract classes.

Version 2.7:

#!/usr/bin/python2.7

#-*-Coding:utf-8-*-

Import ABC # imports the ABC module, which can be used to define abstract classes and abstract methods.

Class Read_and_write_devices:

__metaclass__ = abc. Abcmeta #定义这个类为抽象类

All_type = "Rw_divce"

@abc. Abstractmethod # Defining abstract methods in abstract classes

def read (self):

Pass

@abc. Abstractmethod # Defining abstract methods in abstract classes

def write (self):

Pass

Class Scsi_harddisk (read_and_write_devices):

def read (self):

Print "SCSI hard drive Read method"

def write (self):

Print "SCSI hard disk Write method"

Class Memory_ram (read_and_write_devices):

def write (self):

Print "How To write Memory"


Version 3:

Import ABC

Class Read_and_write_devices (METACLASS=ABC. Abcmeta):

All_type = "Rw_divce"

@abc. Abstractmethod

def read (self):

Pass

@abc. Abstractmethod

def write (self):

Pass

Class Scsi_harddisk (read_and_write_devices):

def read (self):

Print "SCSI hard drive Read method"

def write (self):

Print "SCSI hard disk Write method"

Class Memory_ram (read_and_write_devices):

def write (self):

Print "How To write Memory"


A @abc is added to the method of the abstract class. Abstractmethod this adorner after the method in this abstract class is turned into an abstract method, if other classes inherit this abstract class, it must be to implement the abstract class of the method of abstraction!! If the abstract method defined in the abstract class is not met, the class will not be able to create the object!!

Let's take a look at the example above and first define a class called read_and_write_devices, which can read and write devices. Hard disk and memory are both readable and writable devices, now define a memory class to control the memory read and write, a hard disk class to control the hard disk read and write, both memory and hard disk must have two methods, namely read and write, Scsi_harddisk class and Memory_ Ram one for the operation of memory, and the other to operate the hard disk, these two class colleagues inherit read_and_write_devices this abstract class, where the Scsi_harddisk class satisfies the abstract class in the abstract read and write methods, and Memory_ The Ram class only satisfies the abstract write method in the abstract class, and then uses these two classes to create the object and see what the result will be.

Disk1 = Scsi_harddisk ()

Disk1.read ()

>>>SCSI hard Disk Read method

Scsi_harddisk This class can create an object. (This class implements the Read method and the Write method abstracted by abstract classes).

Next try Memory_ram whether the object can be generated.

Mem1 = Memory_ram ()

The resulting output is as follows:

Traceback (most recent):

File "/users/macbook/pycharmprojects/untitled1/8day/test.py", line +, in <module>

Mem1 = Memory_ram ()

Typeerror:can ' t instantiate abstract class Memory_ram with abstract methods read

Process finished with exit code 1

In Memory_ram, this class has made an error in instantiating the object, because the class did not implement the read_and_write_devices abstraction of the abstract class, which resulted in the memory_ of the two memory_ram. Ram This class has no way of instantiating an object.


Why use interfaces to inherit this feature?

The interface (that is, the abstract class above) extracts a group of common functions, and then allows functions (methods) that inherit the interface class (abstract class) to implement functions in the interface class.

This is done to achieve a concept called "normalization".

The so-called normalization idea is that as long as the class is implemented through the same interface class (abstract class), the objects produced by these classes are the same as the methods used.

To be more understandable, it is that we no longer need to be concerned about which class the object is generated from, as long as we know what these objects are capable of.

For example: We have a car interface, which defines all the functions of the car, and then by the Honda Car class, Audi car class, Volkswagen class, they all realize the car interface, so it is good to do, we just have to learn how to drive a car, then whether it is Honda, Audi, or Volkswagen we will open, There's no need to worry about what kind of car I drive, and how it's done (function calls) are the same.


This article is from the "Rebirth" blog, make sure to keep this source http://suhaozhi.blog.51cto.com/7272298/1912947

8.python Face Object Part.4 (the thought design of interface inheritance, this kind of thing is also called abstract class)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.