以下是我想了解内部类调用语法的做的一个练习,有些疑惑请大家指教interface Device{
  void test1();
}public class Shiyan3{
  private class A implements Device{     
    private A(){       
      test1();
    }
    public void test1(){
      System.out.println("I'm class A");
    }
    public void test3(){
      System.out.println("I'm class A 2");
    }
  }
  public Device test2(){        
    return new A();
  } 
  public static void main(String[] args){
   new Shiyan1().test2();          //I'm class A]
   new Shiyan1().test2().test3();  //编译出错,提示找不到方法test3()
   new Shiyan1().test2().test1();   //编译出错,提示test1()在不可访问的类或接口中
  }
}
编译结果很明显
 public Device test2(){        
    return new A();
  } 
返回的不是类A的实例,倒像是接口Device的实例(当然接口没有实例,呵呵)。我想请教下为什么会出现这种结果,以及在本练习中如何调用才能真正调用到类A的实例,谢谢

解决方案 »

  1.   

    1.//编译出错,提示找不到方法test3()
    这是因为你test2方法的返回签名是Device接口,所以只具有Device接口的“能力”2.//编译出错,提示test1()在不可访问的类或接口
    完全是你的笔误,前面是Shiyan3,后面成了Shiyan1
      

  2.   

    public Device test2(){        
        return new A();
      } 
    改为public A test2(){        
        return new A();
      }
    是一样的结果,我开始用的就是public A test2(),后来看了某些相似练习才改为public Device test2(),只是我的不成功。
    至于您说的
    2.//编译出错,提示test1()在不可访问的类或接口 
    完全是你的笔误,前面是Shiyan3,后面成了Shiyan1这个我没有笔误,我是为了调试才特意用了几个方法名作不同情况的解析
      

  3.   

    哎,抱歉,是我看错了。
    今天练习作晕了,呵呵还有就是,我看了不少相似的练习,都是用public Device test2()而不是用public A test2(),个人觉得public A test2()更具有可读性。不过以下的练习用这种方法就不行。
    interface Teachable{
      void work();
    }class Programmer{
      private String name;
      public Programmer(){}
      public Programmer(String name){
        this.name=name;
      }
      public void work(){
        System.out.println(this.name+ "在编程序……");
      }
      public String getName(){
        return name;
      }
    }class ComputerTeacher extends Programmer{
      public ComputerTeacher(){}
      public ComputerTeacher(String name){
        super(name);
      }
      private void teach(){
        System.out.println(getName()+ "在讲台上讲解……");
      }
      private class Colsure implements Teachable{
        public void work(){   
          teach();
        }
      }
    /*
      public void get(){       
        new Colsure().work();
      }
    */
      public Teachable get(){   //这里如果用public Colsure get()则会在下边调用方法get()时编译出错
         return new Colsure();
      }
    }public class Bibao1{
      public static void main(String[] args){
        ComputerTeacher a=new ComputerTeacher("大狗");
        a.work();          //大狗在编程序……
        a.get().work();    //大狗在讲台上讲解……
      }
    }
      

  4.   

       new Shiyan1().test2().test3();  //编译出错,提示找不到方法test3()
    这里的原因是因为,你这个返回对象的方法public Device test2(){        
        return new A();
      } 
    的返回类型是Device 这个接口,从这个接口的角度看过来,他只包含test1()方法,而test3()是属于A类的。所以对于Device接口来说,他是不可见的。如果你把返回类型改为A
    即:
    public A test2(){        
        return new A();
      } 
    那么就可以了。   new Shiyan1().test2().test1();   //编译出错,提示test1()在不可访问的类或接口中
      朋友,你这里报的错误我到没有见到,不过这里和上面难道不是同一个道理?
      

  5.   

    private class Colsure implements Teachable{
        public void work(){   
          teach();
        }
      }朋友,这里的问题是因为你这里定义的类是一个private的,因为你的main方法在另外一个类中,所以你的内部类对于main方法所在类的来讲是一个不可见的。
      

  6.   

    5楼的例子
    a.get()的时候要加载get()返回值的Class吧
    你Colsure是private的,所以加载的时候会找不到这个类吧
    你把Colsure改成public 试试
      

  7.   

    谢谢楼上两位,将Colsure改为public确实可以使用public Colsure get()来调用a.get().work(); 了
    也正如8楼朋友所说:“因为你的main方法在另外一个类中,所以你的内部类对于main方法所在类的来讲是一个不可见的。”
    谢谢!