Python polymorphism

#!/usr/bin/python3
# classes.py by Bill Weinman [http://bw.org/]
# This is an exercise file from Python 3 Essential Training on lynda.com
# Copyright 2010 The BearHeart Group, LLC


class Duck:
    
    def __init__(self):
        
        print('constructor')
       
    def quack(self):
        print('Quaaack!  like a duck')


    def walk(self):
        print('Walks like a duck')
    
class Dog:
    
    def __init__(self):
        
        print('constructor')
       
    def quack(self):
        print('Bark!  like a dog')


    def walk(self):
        print('Walks like a dog')     
def in_forest(dog):
    dog.quack()
    dog.walk()


def in_water(cat):
    cat.quack()
    cat.walk()    

def main():
    donald = Duck()
    poo=Dog()
    in_forest(donald)
    in_water(poo)
    
    for o in(donald,poo):
        o.quack()
        o.walk()   
       
    
   


if __name__ == "__main__": main()



constructor
constructor
Quaaack!  like a duck
Walks like a duck
Bark!  like a dog
Walks like a dog
Quaaack!  like a duck
Walks like a duck
Bark!  like a dog
Walks like a dog


python的类型太弱了

阅读更多
个人分类: python
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭