匿名类错误

时间:2013-04-05 16:21:38

标签: java anonymous-class

public interface IMethodCallback 
{
    boolean Execute();
}

...

IMethodCallback callback = new IMethodCallback
            {
                boolean Execute()
                {
                    return false;
                }
            };

这会导致错误。我怎样才能在Java中创建这样的东西?

4 个答案:

答案 0 :(得分:3)

你错过了构造函数的括号:

IMethodCallback callback = new IMethodCallback()
{
    public boolean Execute()
    {
        return false;
    }
};

答案 1 :(得分:2)

您缺少括号()

IMethodCallback callback = new IMethodCsallback() {
          // your impl method
};

答案 2 :(得分:2)

正如其他人提到的那样,new IMethodCallback之后你缺少括号。但是您还需要声明实现方法Execute() public,因为这是实现接口的方法。界面中的方法隐式public

IMethodCallback callback = new IMethodCallback()
{
    // Implements an interface; must be public
    public boolean Execute()
    {
        return false;
    }
};

答案 3 :(得分:1)

IMethodCallback callback = new IMethodCallback() //You have missed the parenthesis of constructor.
{
     public boolean Execute() {
         return false;
     }
 };

并遵循java命名约定( Camel Case