日期:2014-05-20  浏览次数:20754 次

哪位高手举个经典的多态的例子
我觉得多态没太大的用处...哪位高手能够举个例子说明下...谢谢.

------解决方案--------------------
不用管它。实践多了自然就明白了。
------解决方案--------------------
abstract class Logger {
public void log(String message);
public void log(int errorCode);
public void log(String message, int errorCode);
public void log(String message, int errorCode, Throwable t);
}

这就是多态。你可以根据实际情况选择你需要的 log 方法来使用。
------解决方案--------------------
4个裤衩的 多态和重载都不分~~
多态就是有多种形态
比如LZ 可以是男人形态 也可以是人形态 也可以是生物形态

------解决方案--------------------

------解决方案--------------------
class A{
public String f(D obj){return ( "A and D ");}
public String f(A obj){return ( "A and A ");}
}
class B extends A{
public String f(B obj){return ( "B and B ");}
public String f(A obj){return ( "B and A ");}
}
class C extends B{}
class D extends B{}

class TestComplexPoly{
public static void main(String[] args){
A a1 = new A();
A a2 = new B();
B b = new B();
C c = new C();
D d = new D();
//System.out.println(a1.f(b)); // A and A
//System.out.println(a1.f(c)); //A and A
//System.out.println(a1.f(d)); //A and D
--------------------------
System.out.println(a2.f(b)); //B and A
System.out.println(a2.f(c)); //B and A
问题就是这两个为什么会是B and A 呢
--------------------------

//System.out.println(a2.f(d)); //A and D
//System.out.println(b.f(b)); //B and B
//System.out.println(b.f(c)); //B and B
//System.out.println(b.f(d)); //A and D
}
}