Object-oriented interface inheritance inherit the python interfaces [seven]

The python interface inheritance

 

Interface inheritance

Interface inheritance is the (base class) parent class defined the two function attributes (interfaces), all subclasses must have these two functions properties, are indispensable, not to say that the provincial code, is used to make a mandatory constraint

Base class methods without specific implementation, it is only a specification

1.1 implements a concept of everything is a file

Copy the code
class Disk:
    def read(self):
        pass
    def write(self):
        pass
class Cdrom:
    def read(self):
        pass
    def write(self):
        pass
class Mem:
    def read(self):
        pass
    def write(self):
        pass
Copy the code

1.2 a base class can be defined, to improve the above code

Copy the code
class Allfile:
    def read(self):
        pass
    def write(self):
        pass
class Disk(Allfile):
    def read(self):
        print("disk read")
    def write(self):
        print("disk write")
class Cdrom(Allfile):
    def read(self):
        print("cdrom read")
    def write(self):
        print("cdrom write")
class Mem(Allfile):
    def read(self):
        print("mem read")
    def write(self):
        print("mem write")
Copy the code

Subclass 1.3 but you may not be in accordance with the provisions of the cards, Mem just do not listen, he does not write attributes defined functions, and then it will find from the parent class. The parent class is pass

Copy the code
class Allfile:
    def read(self):
        pass
    def write(self):
        pass
class Disk(Allfile):
    def read(self):
        print("disk read")
    def write(self):
        print("disk write")
class Cdrom(Allfile):
    def read(self):
        print("cdrom read")
    def write(self):
        print("cdrom write")
class Mem(Allfile):
    def read(self):
        print("mem read")
m1=Mem()
m1.read()
m1.write()

C:\python35\python3.exe D:/pyproject/day25/接口继承.py

mem read
Copy the code

1.4 python so there is a special module to achieve this mandatory constraint subclass module called abc

Import module ABC, then the parent class two attributes plus the decorator, then less if subclass attributes, given directly, and thus mandatory constraints subclass must have two methods of the parent class

Copy the code
import abc
class Allfile(metaclass=abc.ABCMeta):
    @abc.abstractstaticmethod
    def read(self):
        pass
    @abc.abstractstaticmethod
    def write(self):
        pass
class Disk(Allfile):
    def read(self):
        print("disk read")
    def write(self):
        print("disk write")
class Cdrom(Allfile):
    def read(self):
        print("cdrom read")
    def write(self):
        print("cdrom write")
class Mem(Allfile):
    def read(self):
        print("mem read")
m1=Mem()

TypeError: Can't instantiate abstract class Mem with abstract methods write
Copy the code

After 1.5 subclass Mem also add this method can write normal operation

Copy the code
import abc
class Allfile(metaclass=abc.ABCMeta):
    @abc.abstractstaticmethod
    def read(self):
        pass
    @abc.abstractstaticmethod
    def write(self):
        pass
class Disk(Allfile):
    def read(self):
        print("disk read")
    def write(self):
        print("disk write")
class Cdrom(Allfile):
    def read(self):
        print("cdrom read")
    def write(self):
        print("cdrom write")
class Mem(Allfile):
    def read(self):
        print("mem read")
    def write(self):
        print("mem write") 
M1 = mem ()
m1.read()
m1.write()

mem read

mem write
Copy the code

Interface inheritance

Interface inheritance is the (base class) parent class defined the two function attributes (interfaces), all subclasses must have these two functions properties, are indispensable, not to say that the provincial code, is used to make a mandatory constraint

Base class methods without specific implementation, it is only a specification

1.1 implements a concept of everything is a file

Copy the code
class Disk:
    def read(self):
        pass
    def write(self):
        pass
class Cdrom:
    def read(self):
        pass
    def write(self):
        pass
class Mem:
    def read(self):
        pass
    def write(self):
        pass
Copy the code

1.2 a base class can be defined, to improve the above code

Copy the code
class Allfile:
    def read(self):
        pass
    def write(self):
        pass
class Disk(Allfile):
    def read(self):
        print("disk read")
    def write(self):
        print("disk write")
class Cdrom(Allfile):
    def read(self):
        print("cdrom read")
    def write(self):
        print("cdrom write")
class Mem(Allfile):
    def read(self):
        print("mem read")
    def write(self):
        print("mem write")
Copy the code

Subclass 1.3 but you may not be in accordance with the provisions of the cards, Mem just do not listen, he does not write attributes defined functions, and then it will find from the parent class. The parent class is pass

Copy the code
class Allfile:
    def read(self):
        pass
    def write(self):
        pass
class Disk(Allfile):
    def read(self):
        print("disk read")
    def write(self):
        print("disk write")
class Cdrom(Allfile):
    def read(self):
        print("cdrom read")
    def write(self):
        print("cdrom write")
class Mem(Allfile):
    def read(self):
        print("mem read")
m1=Mem()
m1.read()
m1.write()

C:\python35\python3.exe D:/pyproject/day25/接口继承.py

mem read
Copy the code

1.4 python so there is a special module to achieve this mandatory constraint subclass module called abc

Import module ABC, then the parent class two attributes plus the decorator, then less if subclass attributes, given directly, and thus mandatory constraints subclass must have two methods of the parent class

Copy the code
import abc
class Allfile(metaclass=abc.ABCMeta):
    @abc.abstractstaticmethod
    def read(self):
        pass
    @abc.abstractstaticmethod
    def write(self):
        pass
class Disk(Allfile):
    def read(self):
        print("disk read")
    def write(self):
        print("disk write")
class Cdrom(Allfile):
    def read(self):
        print("cdrom read")
    def write(self):
        print("cdrom write")
class Mem(Allfile):
    def read(self):
        print("mem read")
m1=Mem()

TypeError: Can't instantiate abstract class Mem with abstract methods write
Copy the code

After 1.5 subclass Mem also add this method can write normal operation

Copy the code
import abc
class Allfile(metaclass=abc.ABCMeta):
    @abc.abstractstaticmethod
    def read(self):
        pass
    @abc.abstractstaticmethod
    def write(self):
        pass
class Disk(Allfile):
    def read(self):
        print("disk read")
    def write(self):
        print("disk write")
class Cdrom(Allfile):
    def read(self):
        print("cdrom read")
    def write(self):
        print("cdrom write")
class Mem(Allfile):
    def read(self):
        print("mem read")
    def write(self):
        print("mem write") 
M1 = mem ()
m1.read()
m1.write()

mem read

mem write
Copy the code

Guess you like

Origin www.cnblogs.com/youxiu123/p/11481175.html