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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 刘国涛 中级黑马   /  2013-3-19 15:01  /  1168 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 刘国涛 于 2013-3-19 17:24 编辑

interface Inter
{
void show(int a,int b);
void func();
}
class Demo implements Inter
{
public static void main(String[] args)
{
  //调用两个函数,用匿名内部类
  Inter in = new Inter()
  {
   public void show(int a,int b)
   {
    System.out.println(a+b);
   }
   public void func()
   {
    System.out.println("function");
   }
  };
  in.show(4,5);
  in.func();
  }
}
为什么编译出错了呢?求解

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

3 个回复

倒序浏览
本帖最后由 michaelchen 于 2013-3-19 15:29 编辑

interface Inter
{
void show(int a,int b);
void func();
}
class Demo implements Inter //用类实现接口需要覆写其方法
{
public static void main(String[] args)
{
  //调用两个函数,用匿名内部类
  Inter in = new Inter()//尝试用接口创建对象
  {
   public void show(int a,int b)
   {
    System.out.println(a+b);
   }
   public void func()
   {
    System.out.println("function");
   }
  };
  in.show(4,5);
  in.func();
  }
}
}

楼主,你在尝试用接口创建对象,根源就在这里

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

回复 使用道具 举报
首先 你定义的Inter是一个接口,不是一个内部类
第二你的Demo 类实现了Inter接口但是却未实现接口中的两个方法
回复 使用道具 举报
我已经明白了,谢谢各位!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马