我可以做一个内联匿名类来实现Java接口吗

我可以做一个内联匿名类来实现Java接口吗

本文介绍了在Jython中,我可以做一个内联匿名类来实现Java接口吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,我可以说

In Java, I can say

Thread t = new Thread(){
    public void run(){ // do stuff }
}

(或类似的东西)来内联声明一个匿名类.这对于制作事件处理程序或其他回调类的东西(以任何明智的语言.

(or something much like that) to declare an anonymous class inline. This is most useful for making event handlers or other callback sorts of things, that in any sensible language wouldn't require an object to own them in the first place.

我在Jython中遇到了同样的问题-我想定义一个事件处理程序,但是我不想构建一个完整的独立类来这样做.

I face the same problem in Jython -- I want to define an event handler, but I don't want to build a whole standalone class to do so.

理想情况下,我只能传递一个lambda并完成它,但是如果可能的话,我将无法在文档中的任何地方找到它.

Ideally, I'd just be able to pass a lambda and be done with it, but if this is possible, I can't find it anywhere in the docs.

推荐答案

以下是我原始答案的背景阅读链接:

Here are the background-reading links from my original answer:

请阅读以下内容: Python是否具有类似于匿名的东西Java的内部类?

然后,阅读以下内容: Python是否有匿名类?

Then, read this: Does Python have anonymous classes?

最后,请阅读以下内容:是否可以创建匿名Python中的对象?

Finally, read this: Is it possible to create anonymous objects in Python?

[原始答案的改进]

Jython在适当的时候会将函数隐式转换为事件处理程序.因此,您可以在其中一种方法中编写此代码:

Jython implicitly converts a function to an event handler when appropriate. So, you can write this inside of one of your methods:

def _(self, event):
    print 'Cancel button was clicked!'
    if self.task is not None:
        self.task.cancel()
cancelButton.setOnAction(lambda event: _(self, event))

这种方法提供了匿名内部类的三个理想特征.

This approach provides three of desirable characteristics of an anonymous inner class.

(1)高度本地化.处理程序代码位于分配处理程序的代码旁边.

(1) It is highly localized. The handler code is next to the code that assigns the handler.

(2)它是自我意识的,表示您具有self并且可以访问包含对象的所有成员.

(2) It is self aware, meaning you have self and can access to all of the members of the containing object.

(3)(几乎)是匿名. Python强迫我为多行函数选择一个名称,但是它也允许我重用该名称.因此,只要我立即使用它们,就可以定义许多称为_的局部函数. (用相同的名称定义第二个功能将使该名称无法访问第一个功能.)因此,我不必被迫发明无限的唯一名称.

(3) It is (almost) anonymous. Python forces me to choose a name for multi-line function, but it also allows me to reuse that name. So, I can define as many local functions called _ as long as I use them right away. (Defining a second function by the same name makes the first function inaccessible by that name.) So, I am not forced to invent unique names ad-infinitum.

您还可以将此模式打包在装饰器中,我认为这可以澄清其意图:

You can also package this pattern in a decorator, which I think clarifies the intent a bit:

@LocalEventHandler(self, cancelButton.setOnAction)
def _(self, event):
    print 'Cancel button was clicked!'
    if self.task is not None:
        self.task.cancel()

装饰器实现如下:

class LocalEventHandler(object):
    '''This decorator turns a local function into a *self aware* event handler.'''

    def __init__(self, context, assignHandlerFunc):
        self.context = context
        self.assignHandlerFunc = assignHandlerFunc

    def __call__(self, handler):
        self.assignHandlerFunc(lambda event: handler(self.context, event))
        return handler

这篇关于在Jython中,我可以做一个内联匿名类来实现Java接口吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 03:41