Import ABCMeta ABC from, AbstractMethod
class the Pet (Object, the metaclass that = ABCMeta):
'' 'pet' ''
DEF the __init __ (Self, Nickname):
self._nickname = Nickname
@abstractmethod
DEF make_voice (Self):
'' 'sound' ''
Pass
class dog (the Pet):
'' 'dog' ''
DEF make_voice (Self):
Print ( "% S: bark Wang ..."% self._nickname)
class cat (the Pet):
'' 'cats' ''
DEF make_voice (Self):
Print ( "% S: meow meow" self._nickname%)
DEF main ():
Qucik Facts Pets = [Dog ( 'Cai'), Cat ( 'Katy'), Dog ( ' rhubarb ')]
for Qucik Facts Pets in PET:
PET.make_voice ()
IF __name__ __ == '__ main__':
main ()
1. abstract class that can not be instantiated
2. ABC module ABCMeta inherited metaclass in a class by abstractmethod decorators and modified, it becomes an abstract class
3. The class is drawn from the same content from a bunch of objects, then the abstract class is a class from a pile extracted from the same content, including data attributes and function attributes.
4. The function attributes abstract class, defines specific, not implemented
python learning abstract class
Guess you like
Origin www.cnblogs.com/jinpingzhao/p/12624251.html
Recommended
Ranking