Object-oriented: polymorphism and encapsulation

Polymorphism refers to a class of things have a variety of forms: python naturally-polymorphic 
example:
class wechat:
     DEF the Pay (Self, Money):
         Print ( ' pay% s money ' % Money) 

class Alipay:
     DEF the Pay (Self, Money):
         Print ( ' pay% s money ' % Money) 

class Applepay:
     DEF the Pay ( Self, money):
         Print ( ' pay money to% s ' % money) 

# Python call directly pass arguments to the payment of all classes can call the above method 
DEF the pay (payment, maoney): 
    payment.pay (maoney)

Polymorphic: in java, because it is strong data typing, calling these classes need to create a parent to them, it means that will allow the assignment of a subclass type pointer to a pointer to the parent class type
class Payment:#父类
    pass

class Wechat(Payment):
    def pay(self,money):
        print('支付%s钱'%money)

class Alipay(Payment):
    def pay(self,money):
        print('支付%s钱'%money)

class Applepay(Payment):
    def pay(self,money):
        print('支付%s钱'%money)
        
def pay(Payment payment,int maoney):#Payment here is the parent of Payment, the only way to use to subclass Wechat, etc. In java 
    payment.pay (maoney)

 

Package:

  Generalized: Object-oriented package is to protect the code, object-oriented thinking itself it is a kind, just to make myself an object can call its own methods in the class is a kind of package

  Narrow: is one of the three object-oriented features, properties and methods to hide, can not let you call outside, so you can not see

Usage: front double underlined name

class the Person:
     DEF  the __init__ (Self, name, Money): 
        the self.name = name 
        Self. __money = Money         # encapsulated, may be used in the class 
    DEF  __drawing (Self):             # is encapsulated, may be used in the class 
        Print ( ' I paint very well, but secrecy ' )
     DEF FUNC (Self): 
        . Self __drawing ()             # call methods encapsulated 
        Print . (Self __money )          # call encapsulated property 

Aike = the Person ( ' Aike ', 1000 )
 Print (aike.name)
 Print (Aike. __Dict__ ) # encapsulated attribute to the class name plus the name of the attribute exists among the dictionary 
Print (aike._Person__money) # with the key can view the value of the package, but this is not recommended use 
aike.func () # call the normal method 
Print (aike.money) # is encapsulated, can not be called, will complain 
Aike. __drawing # is encapsulated, can not be called, will complain

 

Guess you like

Origin www.cnblogs.com/aizhinong/p/11469155.html