Passing code as function arguments

后端 未结 6 1588
星月不相逢
星月不相逢 2021-01-23 22:56

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.

SwingU         


        
6条回答
  •  無奈伤痛
    2021-01-23 23:10

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

    It is an instance of a user-defined class. The only difference is that this class does not have a name *.

    It is a real instance of a class, though - it can do most of the things a named class can do. Among other things, it can provide implementations of methods of its base class or an interface, which is what is used to pass "a piece of executable code" to a method.

    * At least, not a user-visible one: Java compiler does assign each anonymous class an internal name, which usually contains a dollar sign.

提交回复
热议问题