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
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
1.2 a base class can be defined, to improve the above 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")
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
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
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
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
After 1.5 subclass Mem also add this method can write normal operation
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
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
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
1.2 a base class can be defined, to improve the above 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")
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
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
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
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
After 1.5 subclass Mem also add this method can write normal operation
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