您的位置:程序门 -> java -> j2se / 基础类



哪位高手举个经典的多态的例子


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


哪位高手举个经典的多态的例子
发表于:2007-01-31 22:27:00 楼主
我觉得多态没太大的用处...哪位高手能够举个例子说明下...谢谢.
发表于:2007-01-31 23:22:441楼 得分:0
不用管它。实践多了自然就明白了。
发表于:2007-02-01 11:26:162楼 得分:0
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   方法来使用。
发表于:2007-02-01 11:57:163楼 得分:0
4个裤衩的   多态和重载都不分~~
多态就是有多种形态
比如lz   可以是男人形态   也可以是人形态   也可以是生物形态  
发表于:2007-02-01 12:01:594楼 得分:0
发表于:2007-02-01 12:05:435楼 得分:0
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                                                                                            
        }
}

发表于:2007-02-01 12:24:596楼 得分:0
to   insiku(㊣瀟湘夜雨㊣)
    什么时候从delphibbs中转过来的?
发表于:2007-02-01 13:05:207楼 得分:0
object[]   obj   =   new   object[10];
obj[0]   =   new   string( "11 ");

发表于:2007-02-01 13:18:548楼 得分:0
to   shengli_liao(我是谁?)  
???
认错人了吧~~~我不做delphi
发表于:2007-02-01 13:20:109楼 得分:0
光研究多态是没有的
多态只是为了动态绑定提供辅助~~~
动态绑定才是关键用处
发表于:2007-02-02 19:10:1610楼 得分:0
  回复人:insiku(㊣瀟湘夜雨㊣)   (   二级(初级))   信誉:100   2007-02-01   11:57:16   得分:0
?  
4个裤衩的   多态和重载都不分~~
多态就是有多种形态
比如lz   可以是男人形态   也可以是人形态   也可以是生物形态

我们老师总说重载就是多态的表现.......
发表于:2007-02-02 21:17:3111楼 得分:0
> > 我们老师总说重载就是多态的表现.......
重载也可以看做多态,不过是编译时多态。
发表于:2007-02-02 22:11:4512楼 得分:0
system.out.print
够不够经典
发表于:2007-02-02 22:21:2313楼 得分:0
重载怎么会是多态的表现??

赎我愚昧~~
发表于:2007-02-02 22:46:3414楼 得分:0
c++也有多态嘛,就是方法的重载
但是java的接口使得多态更加多姿多彩了
发表于:2007-02-02 23:09:4315楼 得分:0
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();
        }
}
运行一下就知道多态是什么了
发表于:2007-02-02 23:39:2416楼 得分:0
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));

这算不算多态?


快速检索

最新资讯
热门点击