day25 interface class abstract class encapsulates

review:

# Inheritance: What is the relationship between what 
# single inheritance *****
# first then inherit the abstract, the same code between several classes abstract, and become the parent class
# subclass he did not name, you can use the parent class methods and attributes
# if they have a sub-category, it must be first with their
# in class when using self, be sure to look at self who point
# multiple inheritance ***
# new classes and classic classes:
# multiple inheritance to find the name order: the new class breadth-first, depth-first classic
# new class has a class name .mro method, see the breadth-first order of succession
# python3 there is a super way to find a class based on the priority order of succession breadth

# Interface class: Python native does not support 
# abstract class: Python native support
# interface class and abstract class is just an idea
# specification: interface class or abstract class can
# Interfaces support multiple inheritance, all methods in the interface class are must not be achieved - the Java
# abstract class does not support multiple inheritance, abstract class method can achieve some code - the Java
from abc Import AbstractMethod, ABCMeta
class Payment (the metaclass that = ABCMeta): # default metaclass metaclass of the type
@ AbstractMethod
DEF pay (Self, Money): Pass # not implement this method
class wechat (payment):
DEF pay (Self, Money):
Print ( 'has paid% s meta micro channel'% Money)
class Alipay (payment):
DEF pay (Self, Money):
Print ( 'has been used Alipay the% s element'% Money)
class Applepay (payment):
DEF pay (Self, Money):
Print ( 'has paid% s element with applepay'% Money)
DEF the pay (pay_obj, Money): # unified pay entrance
pay_obj.pay(money)

app = Applepay()
p = Payment()

# 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 the definition of function '
with Open (' filaname ') AS f:
Pass

@ abc.abstractmethod # define abstract methods without having to implement functions
DEF the write (Self):
' subclass must define the write function '
Pass

class Txt (All_file): # child class 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 an abstract class, but must define read and write methods
DEF read (Self):
( 'reading method process data') Print

DEF write (Self):
( 'reading method process data') Print

wenbenwenjian = Txt ()

yingpanwenjian = Sata ()

jinchengwenjian = process ()

# so we are all 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)

Import AbstractMethod ABC from, ABCMeta 
class Swim_Animal (= the metaclass that ABCMeta):
@abstractmethod
DEF Swim (Self): Pass

class Walk_Animal (= the metaclass that ABCMeta):
@abstractmethod
DEF Walk (Self): Pass

class Fly_Animal (= the metaclass that ABCMeta):
@abstractmethod
DEF Fly (Self): Pass

class Tiger (Walk_Animal, Swim_Animal):
DEF Walk (Self):
Pass
DEF Swim (Self):
Pass
class OldYing (Fly_Animal, Walk_Animal): Pass
class Swan (Swim_Animal, Walk_Animal, Fly_Animal): Pass
# interface class just to meet the interface specification ideological isolation principle of object-oriented development



# Abstract class: Specification
# Normally single inheritance function can be achieved are the same, so the parent class can have some simple basic realization of
case # multiple inheritance because the function is more complex, it is not easy to abstract the same functionality the specific implementation written in the parent class
# abstract class or interface classes: object-oriented development specification of all interfaces and abstract classes can not be instantiated class
# the Java:
# inside the Java class inherits all are single inheritance, so the abstract class the perfect solution to single inheritance regulatory issues demand in the
# but for multiple inheritance demand, because they do not support java itself syntax, so creating an interface Interface concept to solve multiple inheritance regulatory issues

# python
no interface class # python in:
# python comes with multiple inheritance we directly use class to implement the interface class
# python in support abstract class: generally single inheritance can not be instantiated
# and python code can be achieved

Object-oriented package broadly on #: protection code, object-oriented thinking is itself a 
# just to make their own objects can call methods in the class

package on # narrowly - one of three characteristics of object-oriented
# property and methods to hide from you see the
class the Person:
__key = 123 # private static properties
DEF __init __ (Self, name, passwd):
self.name name =
Self .__ passwd # passwd = private property
def __get_pwd (self): # private method
return self .__ passwd # just use a private property within the class, will automatically bring the class name _
def login (self): # normal method calls a private method
Self .__ get_pwd ()
Alex the Person = ( 'Alex' , 'alex3714')
Print (alex._Person__passwd) # _ __ class name attribute name
Print (alex.get_pwd ())

# all private variables are on the left with double underline
the private property of the object #
# class the private method
# class static private property
for external use # all private are not in class

# Polymorphic python naturally-polymorphic 
# DEF FUNC (int NUM, STR name):
# Pass
#
# FUNC ( 'Alex', 2)
# class Payment: Pass

# class Alipay ():
# DEF Pay (Self, Money) :
# Print ( 'has already paid% s yuan Alipay'% Money)
#
# class applepay ():
# DEF the pay (Self, Money):
# Print ( 'already paid with applepay the% s Element'% Money)
#
# def pay (pay_obj, money) : # unified pay entrance normalized design
# pay_obj.pay (Money)
#
# the pay ()

# What is polymorphic
# python dynamic strongly typed language
# duck typing
# tuple List
# does not advocate according to the resulting inherited similar
# I just own my own code to achieve it.
# If two classes just like, does not produce brotherhood subclasses of the parent class, but the duck type
# list tuple this similarity, is write the code itself bound, rather than by the constraints of the parent class
# Advantages: no loose coupling between each class similar impact
# drawback: too casual, can only rely on conscious

class List ():
DEF __len __ (Self): Pass
class Tuple ():
DEF __len __ (Self): Pass

DEF len (obj):
return obj .__ len __ ()

L = Tuple ()
len (L)
#
# # strongly typed language polymorphic
# # python language duck type
# interface class and abstract classes point of application in python which is not very necessary




Guess you like

Origin www.cnblogs.com/Murraya/p/11209906.html