因为B是实现接口A的。因为面向对象的多态性,所以B可以当做A类型传递。不知道这样说对不对。

解决方案 »

  1.   

    1、因为B实现了A。
    2、b实际上就是B类的一个对象。
    3、如果你把B的implements A去掉,那么B类就没有实现A类的方法,所以c.str(b)就会出错啦。
      

  2.   

    面向对象的思想来的,
    打个比方:房子是人进的,那么房子不会管进门的是男人,还是女人.
    ------------------------------------------
    interface class A
    {
     void str();
    }
    这是人.
    -------------------------------
    class B implements A
    {
     pulic void str();
      {  
         //程序代码
      {
    }
    这是男人。
    ------------------------
    class C 
    {
      public void Cstr(A a)
      {
        a.str();
       }
    }
    这是房子。
    房子里有一个门 public void Cstr(A a),它对你说只有A(人)能进。
    ----------------------------------------------
    class D
    {
    pulic static viod main (string[] ages)
     {
      C c = new C();
      B b=new B();
      c.str(b);//这里告诉程序我是B(男人,但男人也是人,所以我能进)
     }
    }
    ------------------------------------
    为什么class B implements A除去了implements A,就会抱错
    因为没有了implements A 就表示它不是人,所以不能进,c.str(b)就会报错。^-^
      

  3.   

    换句话说只要implements A 的都可以调用,是人都能进。
      

  4.   

    B相当于一个A的实例,Cstr(A a)中要求传的为接口A类型,所以传B也是可以的
    但去掉了implements A,B 就不再是A的实例,所有会报错。
      

  5.   

    呵呵。受益非浅呀!特别是 gzhiceberg(天晓得) 的解译
      

  6.   

    TO gzhiceberg(天晓得)
      B b=new B();b是B一个对象。他就有这个str()方法对把,传进去就是B这个方法是吧
      

  7.   

    to wcmj(望尘莫及) avi11lang(萧十一郎)
    我看继承了的,就是b到第传进去的什么啊
      

  8.   

    都是些指针
    哪个类new的就是哪个类的对象吧
      

  9.   

    看我的blog,看有没有帮助
    http://blog.csdn.net/yangbc/archive/2005/02/18/292660.aspx
      

  10.   

    TO gzhiceberg(天晓得)
      B b=new B();b是B一个对象。他就有这个str()方法对把,传进去就是B这个方法是吧
    -------------------------------------------------------------------------
    是调用b的str方法,java里面都是动态绑定.^-^
      

  11.   

    in fact, your problem is not how to transfer parameter,it's a problem of inherite!Please examine the following code and notice the relation of A and B
    class A
    {
       public A()
       {
           System.out.println("this is instance of a");
       }
    }
    class B extends A
    {
       public B()
       {
           System.out.println("this is instance of b");
       }
    }
    class test
    {
       public static void main(String[] arg)
       {
          A a=new A();      
       }
    }
    Compile it and run,the result is:this is instance of a
    contine examining the following code:
    class A
    {
       public A()
       {
           System.out.println("this is instance of a");
       }
    }
    class B extends A
    {
       public B()
       {
           System.out.println("this is instance of b");
       }
    }
    class test
    {
       public static void main(String[] arg)
       {
          B b=new B();      
       }
    }
    the result is:this instance of a
                  this instance of b
    then you can see when you instance the class B(SubClass),the first thing of the System is instance the class A(SuperClass)!
    and so,how to instance a class?how can you say a class is instanced?maybe it's constructor method has executed!
    let's continue:
    class A
    {
       public A()
       {
           System.out.println("this is instance of a");
       }
    }
    class B extends A
    {
       public B()
       {
           System.out.println("this is instance of b");
       }
    }
    class test
    {
       public static void main(String[] arg)
       {
          B b=new B(); 
          A a=new A(); 
          a=b;    
       }
    }
    the code will be compile and run well,but
    class A
    {
       public A()
       {
           System.out.println("this is instance of a");
       }
    }
    class B extends A
    {
       public B()
       {
           System.out.println("this is instance of b");
       }
    }
    class test
    {
       public static void main(String[] arg)
       {
          B b=new B(); 
          A a=new A(); 
          b=a;  
       }
    }
    the Java interpreter will produce the following error:test.java:21:incompatible types
    found:A
    request:B
       b=a;
         ~
    why?you can think yourself!