class aa: def __init__(self): pass def get(self): d=5 return d class bb(aa): def __init__(self): aa.__init__(self) def get(self): d=3 return d class dd(aa): def __init__(self): aa.__init__(self) def get(self): d=4 return d def getNum(cc): print cc.get() first=aa() second=bb() third=dd() getNum(first) getNum(second) getNum(third) |
class A { virtual void dosomething()=0; } class B:public A { void dosomething(){}; } |
A* aPtr; aPtr=new B(); aPtr->dosomething(); |
class A: def __init__(self): pass def get(self): d=5 return d class B(A): def __init__(self): A.__init__(self) def get(self): d=3 return d class C(A): def __init__(self): A.__init__(self) def get(self): d=4 return d |
a=A() # 相當(dāng)于C++中的基類指針 b=B() c=C() # 可以這樣實(shí)現(xiàn)多態(tài) a=b # a指向派生對(duì)象b a.get() #調(diào)用B的函數(shù) a=c # a指向派生對(duì)象c a.get() # 調(diào)用C的函數(shù) |
class Father : def __init__(self): pass def drink(self): print 'drink bear...'; def eat(self): print 'eat bread...'; class Son(Father): def __init__(self): Father.__init__(self); def drink(self): print 'drink milk...' if __name__ == "__main__": father = Father(); father.drink(); father.eat(); son = Son(); son.drink(); son.eat(); |
class Father : def drink(self): print 'drink bear...'; def eat(self): print 'eat bread...'; class Son(Father): def drink(self): print 'drink milk...' if __name__ == "__main__": father = Father(); father.drink(); father.eat(); son = Son(); son.drink(); son.eat(); |
歡迎光臨 Chinaunix (http://www.72891.cn/) | Powered by Discuz! X3.2 |