将代码作为函数参数传递 [英] Passing code as function arguments

查看:70
本文介绍了将代码作为函数参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读一本教科书,我想知道我们传递给函数的参数是如何既不是原始的,也不是用户定义的类实例。

I was reading a textbook and I was wondering how come the argument we pass to the function is neither a primitive or an user-defined instance of a class.

SwingUtilities.invokeLater(new Runnable() 
{
    public void run() {
        new ButtonDemo();
    }
});

我了解到这是其中之一。但是在这里它似乎传递了用户定义的构造方法,例如Runnable接口()。看来他们想在以后运行该线程,但什么时候?这是合法的,我认为是,但我从来没有在我的Java类中听说过这样的事情。

I have learned that it was either one of those two. But it seems here that it passes an user-defined constructor method, e.g. Runnable(). It seems they want to run the thread at a later time, but when? And is this even legal, I assume it is, but I never heard of such a thing in my Java class.

推荐答案

这个实际上是传递一个实现Runnable接口的匿名内部类的实例。在 Java教程中了解它们。

This is actually passing an instance of an anonymous inner class implementing the Runnable interface. Read about them in the Java tutorial.

这篇关于将代码作为函数参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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