无法调用匿名类方法 [英] Can't call anonymous class method

查看:134
本文介绍了无法调用匿名类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以想象一些非常有创意的代码在Java:

I can imagine some very creative code in Java:

Object thing = new Object() {
    public void speak() {
        System.out.println("Hi!");
    }
};
thing.speak();

或者,为了获得完全的闭包效果,定义一个 Function interface

Or even, to get the full closure effect, define a Function interface ... you get the idea?

为什么这个代码不起作用?

Why doesn't this code work?

推荐答案

我相信你可以这样做: -

i believe you can do it like this :-

new Object() {    
     public void speak() {
        System.out.println("Hi!");
     }
}.speak();

可能会帮助你。

这篇关于无法调用匿名类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆