class A  has a method a;
class B  has a mehtod b;
a want to invoke(调用) b, but b is not static and A can be create with new keyword;
how to solve??????

解决方案 »

  1.   

    class A  has a method a; 
    class B  has a mehtod b; 
    a want to invoke(调用) b, but b is not static and B can be create with new keyword; 
    how to solve?????? 
      

  2.   

    class A {
       public void a(){
         // System.out.println("method a");
          B b = new B();
          b.b();
       }
    }
    class B {
       public void b(){
          System.out.println("method b");
       }
    }
    public class Test{
       public static void main(String [] args){
           A a = new A();
           a.a();
       }
    }
      

  3.   

    B 既然可以 new,那就 new 一个B的实例呗。 :(
      

  4.   


    a(){
      new B().b();
    }
      

  5.   

    楼主是不是想说不能用new啊?要不然这算什么题目啊?
      

  6.   

    如果是像楼上所说,不能用new
    应该怎么办?
      

  7.   


    答:若不允许使用new B();则这道题想考查内部类了,结构如下:class B
    {
       public void b(){};   class A
       {
         public void a()
         {
            b();//不用new B()而直接调用 b()
         }
       }
    }