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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 城北一直晴。 中级黑马   /  2015-5-28 17:19  /  1098 人查看  /  26 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

interface Inter
{
void method();
}
class Test
{
  Static Inter function()//总说这里错误,大家看看怎么改?
{
   return new Inter()
  {
   public void method()
   {
    System.out.println("method run");
    }
   };
}
}
class InnerClassTest
{
public static void main (String[] args)
{
  Test.function.method();
  }
}

26 个回复

倒序浏览
你这是方法呢?还是定义内部类呢 ?
回复 使用道具 举报
我是小白 我看着就晕了!
回复 使用道具 举报
黯然残影 发表于 2015-5-28 18:13
你这是方法呢?还是定义内部类呢 ?

匿名内部类
回复 使用道具 举报
挖煤黑小伙 发表于 2015-5-28 18:27
我是小白 我看着就晕了!

你是小白技术分还那么多
回复 使用道具 举报
wuyusi 来自手机 中级黑马 2015-5-28 20:35:44
地板
方法上,static后接的是接口名,不错才怪,将inter该成void
回复 使用道具 举报
wuyusi 发表于 2015-5-28 20:35
方法上,static后接的是接口名,不错才怪,将inter该成void

interface Inter
{
void method();
}
class Test
{
//补足代码,通过匿名内部类。
}
class InnerClassTest
{
public static void main(String[] args)
{
  Test.function().method();
  }
}
//这是那个练习的原题。你试试。
回复 使用道具 举报
匿名对象 ,可以理解为 把对象改为了new 类名();的形式
回复 使用道具 举报
膜拜大神!谢谢你!
回复 使用道具 举报
接口不可以创建对象,肯定报错的
回复 使用道具 举报
小白 表示已经看懵逼了········
回复 使用道具 举报
城北一直晴。 发表于 2015-5-28 20:44
interface Inter
{
void method();

之前分析错了,sorry
interface Inter
{void method();
}
class Test
{static Inter function()
        {
                return new Inter()
                        {
                        public void method()
                                {
                        //表达式
                                }
        };
        }
       
}
class InnerClassTest
{
        public static void main(String[] args)
        {
                Test.function().method();
        }
}
回复 使用道具 举报
你把static的s写成大写了,Static不是关键字了
回复 使用道具 举报
914360849 发表于 2015-5-28 21:21
匿名对象 ,可以理解为 把对象改为了new 类名();的形式

interface Inter
{
void method();
}
class Test
{
//补足代码,通过匿名内部类。
}
class InnerClassTest
{
public static void main(String[] args)
{
  Test.function().method();
  }
}
//这是那个练习的原题。你试试。
回复 使用道具 举报
l598790586 发表于 2015-5-28 21:55
接口不可以创建对象,肯定报错的

interface Inter
{
void method();
}
class Test
{
//补足代码,通过匿名内部类。
}
class InnerClassTest
{
public static void main(String[] args)
{
  Test.function().method();//主要要看懂这句话哈。
  }
}
//这是那个练习的原题。你试试。
回复 使用道具 举报
匿名内部类不是用接口创建对象而是借用接口的名字来创建接口的实现类对象,要实现接口里的方法。所以楼主思路是没有问题的。
  Static Inter function()这个function是什么,干嘛要写这个,你写的是匿名内部类如果加了这个function,这个是方法名还是内部类的类名,去掉这个function看看
回复 使用道具 举报
bin2015 发表于 2015-5-29 09:43
匿名内部类不是用接口创建对象而是借用接口的名字来创建接口的实现类对象,要实现接口里的方法。所以楼主思 ...

interface Inter
{
void method();
}
class Test
{
//补足代码,通过匿名内部类。
}
class InnerClassTest
{
public static void main(String[] args)
{
  Test.function().method();//主要要看懂这句话哈。
  }
}
//这是那个练习的原题。你试试。
回复 使用道具 举报
  1. interface Inter
  2. {
  3.         void method();
  4. }
  5. class Test
  6. {
  7.         //补足代码,通过匿名内部类。
  8.          public static void fuction()
  9.         {
  10.                   new Inter(){
  11.                         public  void method()
  12.                         {
  13.                                 System.out.println("匿名内部类");
  14.                         }
  15.                 }.method();
  16.         }
  17. }
  18. class InnerClassTest
  19. {
  20.         public static void main(String[] args)
  21.         {
  22.                 Test.fuction();//主要要看懂这句话哈。
  23.         }
  24. }
复制代码
回复 使用道具 举报
本帖最后由 bin2015 于 2015-5-29 16:17 编辑
  1. <blockquote>new Inter(){            //这里创建的不是Inter接口的对象,创建的是Inter接口实现类的对象。
  2.                         public  void method()     //实现了接口的method方法
  3.                         {
  4.                                 System.out.println("匿名内部类");
  5.                         }
  6.                 }.method();//调用method的方法
复制代码


回复 使用道具 举报
这个题目是接口实现类的局部匿名类部类
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 加入黑马