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的类型太弱了

阅读更多

Polymorphism

04-01

One of your old friends is now developping a new programming language. He want to make it the best programming language ever, or at least the best at most phases. So it is an Object-Oriented Programming language of course. He thinks most OOP languages at present are too inefficient with the handling of inheritance and polymorphism, and he want to make this quicker in his own language. However, he isn't good at algorithm himself, so he comes to you, a well-known ACMer, and is now asking you to help him.nWith the inheritance relations and the classes which has implemented the specific function input, you program is asked to output in which class the function codes will be runned when an object is asked to execute the function. (As a pure OOP language, multiple inheritance is not considered)nnInputnnFor each test case (function), the first line is the name of the base class which has declared the function as an abstract function (or totally virtual function in C++) and an integer N (1 <= N <= 30,000), which is the number of classes which are derived from the base class directly or indirectly, then N lines follow. Each line contains two class names A and B, specifying that class A is derived from class B. All A's will be different in the N lines. After that there is an integer M (0 <= M <= N + 1), the number of classes which have implemented this function. Then M lines follow, each contains one class name C, you may assume that C is either one of the A's or the base class. Finally an integer L (1 <= L <= 30,000) and L lines with each line one class name D, of which an instance is now being asked to executed the function. All class name will be non-space charaters with length no more than 255.nnThese's a blank line between every two cases, processing to the end of file.nnOutputnnFor each test case, in the first line output "Function K" where K is the case index start from 1.nnFor each D, you program should print the name of the class in which the function codes will be executed (D or the nearest base class of D which has implemented the function). If there is not such a class (so the input is an instance of an abstarct class which cannot exist), output "Exception".nnPrint a blank line between every two cases.nnSample InputnnShape 3nTriangle ShapenRectangle ShapenSquare Rectanglen2nTrianglenRectanglen3nTrianglenRectanglenSquarenSample OutputnnFunction 1nTrianglenRectanglenRectangle

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