A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 追梦之旅 初级黑马   /  2014-4-11 23:38  /  803 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

interface Contents{ void go(){};}public class Test1 { public Contents con() { return new Contents() { public void go() { System.out.println("I got it"); } }; }  public static void main(String [] args) { Test1 t = new Test1(); Contents c = t.con(); c.go(); }我想问下此处匿名内部类中的go()方法为什么一定要写明是public的(不然不能运行),难道匿名内部类内部成员默认是private?

1 个回复

倒序浏览

  1. interface Contents
  2. {
  3.         void go(){};
  4. }
  5. public class Test1
  6.         {
  7.         public Contents con() {
  8.                 return new Contents()
  9.                         {
  10.                                 public void go()
  11.                                 {
  12.                                         System.out.println("I got it");
  13.                                 } };
  14.                          }  
  15.         public static void main(String [] args)
  16.         {
  17.                 Test1 t = new Test1();
  18.                  Contents c = t.con();
  19.                  c.go();
  20.         }
复制代码

因为你创建的是接口的子类对象,接口里的方法都是 public 抽象的,所以要重写,一定要大于或者等于该接口的权限
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马