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

解决方案 »

  1.   

    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 方法来使用。
      

  2.   

    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                                              
        }
    }
      

  3.   

    to insiku(㊣瀟湘夜雨㊣)
      什么时候从DelphiBBs中转过来的?
      

  4.   

    Object[] obj = new Object[10];
    obj[0] = new String("11");
      

  5.   

    to shengli_liao(我是谁?) 
    ???
    认错人了吧~~~我不做DELPHI
      

  6.   

     回复人:insiku(㊣瀟湘夜雨㊣) ( 二级(初级)) 信誉:100  2007-02-01 11:57:16  得分:0

    4个裤衩的 多态和重载都不分~~
    多态就是有多种形态
    比如LZ 可以是男人形态 也可以是人形态 也可以是生物形态我们老师总说重载就是多态的表现.......
      

  7.   

    >>我们老师总说重载就是多态的表现.......
    重载也可以看做多态,不过是编译时多态。
      

  8.   

    c++也有多态嘛,就是方法的重载
    但是java的接口使得多态更加多姿多彩了
      

  9.   

    class Animal {
      private String name;
      Animal(String name) {this.name = name;}
     
      public void enjoy(){
        System.out.println("叫声......");
      }
    }class Cat extends Animal {
      private String eyesColor;
      Cat(String n,String c) {super(n); eyesColor = c;}
     
      public void enjoy() {
        System.out.println("猫叫声......");
      }
    }class Dog extends Animal {
      private String furColor;
      Dog(String n,String c) {super(n); furColor = c;}
     
      public void enjoy() {
        System.out.println("狗叫声......");
      }
    }class Bird extends Animal {
     Bird() {
       super("bird");
     }
     public void enjoy() {
        System.out.println("鸟叫声......");
      }
    }class Lady {
        private String name;
        private Animal pet;
        Lady(String name,Animal pet) {
            this.name = name; this.pet = pet;
        }
        public void myPetEnjoy(){pet.enjoy();}
    }public class Test {
        public static void main(String args[]){
            Cat c = new Cat("catname","blue");
            Dog d = new Dog("dogname","black");
            Bird b = new Bird();
            Lady l1 = new Lady("l1",c);
            Lady l2 = new Lady("l2",d);
            Lady l3 = new Lady("l3",b);
            l1.myPetEnjoy();
            l2.myPetEnjoy();
            l3.myPetEnjoy();
        }
    }
    运行一下就知道多态是什么了
      

  10.   

    TO:insiku(㊣瀟湘夜雨㊣)System.out.print(Object obj) {
       print(obj.toString());
    }System.out.print(new String("1"));
    System.out.print(new Integer(1));
    System.out.print(new Double(1));
    System.out.print(new Short(1));这算不算多态?