Object-oriented [8] as much as one of the three characteristics of object-oriented characteristics of the three states python polymorphism

One of the three characteristics of object-oriented python polymorphic

 

Polymorphism

多态的特性是调用不同的子类将会产生不同的行为,而无需明确知道这个子类实际上是什么

It means, different objects call the same method, different behaviors

For example: s1 is the string type, w1 is a list, two completely different objects, they can call len method, the result is different

Polymorphism is actually attached to inherit two meanings: "change" and "extended" itself means you must have a mechanism to choose to change / extend over version, the implementation details polymorphism essentially inherited ;

Method calls the base class of the subclass is instantiated, w1.turn_ice () is called polymorphism;

Copy the code
H20 class: 
    DEF the __init __ (Self, name, temerature): 
        the self.name name = 
        self.temerature = temerature 
    DEF turn_ice (Self): 
        IF self.temerature <0: 
            Print ( "[% S] low temperature frozen" the self.name%) 
        elif self.temerature> self.temerature 0 and <100: 
            Print ( "[% S] liquefied into water" the self.name%) 
        elif self.temerature> 100: 
            Print ( "[% S] temperature is too high water vapor into "the self.name%) 
class water (H20): 
    Pass 
class ice (H20): 
    Pass 
class the steam (H20): 
    Pass 
W1 = water (" water ", 25) 
I1 = ice (" ice ", -20) 
S1 = the steam ( "steam", 3000) 
W1.turn_ice () # call during execution of different objects in the same manner
I1.turn_ice()
s1.turn_ice() 
# or define an interface call to perform the above 
DEF FUNC (obj): 
    obj.turn_ice () 
FUNC (W1) 
FUNC (I1) 
FUNC (S1)
Copy the code

Polymorphism

多态的特性是调用不同的子类将会产生不同的行为,而无需明确知道这个子类实际上是什么

It means, different objects call the same method, different behaviors

For example: s1 is the string type, w1 is a list, two completely different objects, they can call len method, the result is different

Polymorphism is actually attached to inherit two meanings: "change" and "extended" itself means you must have a mechanism to choose to change / extend over version, the implementation details polymorphism essentially inherited ;

Method calls the base class of the subclass is instantiated, w1.turn_ice () is called polymorphism;

Copy the code
H20 class: 
    DEF the __init __ (Self, name, temerature): 
        the self.name name = 
        self.temerature = temerature 
    DEF turn_ice (Self): 
        IF self.temerature <0: 
            Print ( "[% S] low temperature frozen" the self.name%) 
        elif self.temerature> self.temerature 0 and <100: 
            Print ( "[% S] liquefied into water" the self.name%) 
        elif self.temerature> 100: 
            Print ( "[% S] temperature is too high water vapor into "the self.name%) 
class water (H20): 
    Pass 
class ice (H20): 
    Pass 
class the steam (H20): 
    Pass 
W1 = water (" water ", 25) 
I1 = ice (" ice ", -20) 
S1 = the steam ( "steam", 3000) 
W1.turn_ice () # call during execution of different objects in the same manner
I1.turn_ice()
s1.turn_ice() 
# or define an interface call to perform the above 
DEF FUNC (obj): 
    obj.turn_ice () 
FUNC (W1) 
FUNC (I1) 
FUNC (S1)
Copy the code

Guess you like

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