简体   繁体   English

在Jython中,我可以创建一个实现Java接口的内联匿名类吗?

[英]In Jython, can I make an inline anonymous class that implements a Java interface?

In Java, I can say 在Java中,我可以说

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. 这对于制作事件处理程序或其他回调类型的东西非常有用,在任何合理的语言中, 首先不需要对象拥有它们

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. 我在Jython中面临同样的问题 - 我想定义一个事件处理程序,但我不想构建一个完整的独立类来执行此操作。

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. 理想情况下,我只能传递一个lambda并完成它,但如果这是可能的,我无法在文档中的任何地方找到它。

Here are the background-reading links from my original answer: 以下是我原来答案中的背景阅读链接:

Read this: Does Python have something like anonymous inner classes of Java? 阅读: Python是否有类似Java的匿名内部类?

Then, read this: Does Python have anonymous classes? 然后,请阅读: Python是否有匿名类?

Finally, read this: Is it possible to create anonymous objects in Python? 最后,请阅读: 是否可以在Python中创建匿名对象?

[IMPROVEMENT TO ORIGINAL ANSER] [对原始助手的改进]

Jython implicitly converts a function to an event handler when appropriate. Jython在适当的时候隐式地将函数转换为事件处理程序。 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) It is highly localized . (1)高度本地化 The handler code is next to the code that assigns the handler. 处理程序代码位于分配处理程序的代码旁边。

(2) It is self aware , meaning you have self and can access to all of the members of the containing object. (2)它是自我意识的 ,意味着你有self并且可以访问包含对象的所有成员。

(3) It is (almost) anonymous . (3)它(几乎)是匿名的 Python forces me to choose a name for multi-line function, but it also allows me to reuse that name. Python强迫我为多行函数选择一个名称,但它也允许我重用该名称。 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. (使用相同的名称定义第二个函数会使第一个函数无法通过该名称访问。)因此,我没有被迫发明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()

The decorator implementation looks like this: 装饰器实现如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何实例化在Java中实现Java接口的JRuby类 - How can I instantiate a JRuby class that implements a Java interface in Java 实现ActionListener的Java匿名类? - Java anonymous class that implements ActionListener? 匿名 class 实现接口,不能有 arguments - Anonymous class implements interface, cannot have arguments 在Java中,如果一个类实现了一个接口,是否需要编译该接口? - In Java, if a class implements an interface, do I need to compile the interface? 如何在实现接口但不扩展另一个类的Java类中引用super方法? - How can I reference a super method in a Java class that implements an interface but does not extend another class? 我可以使用scala类来实现Java的Java接口吗? - Can I use a scala class which implements a java interface from Java? 在底层实现接口的 Kotlin 内联类 - Kotlin inline class that implements interface under the hood 如何制作一个用两种泛型类型实现一个接口的 Java 类? - How to make a Java class that implements one interface with two generic types? 如何从接口获取实现该接口的类的枚举? - How can I get an enum from an interface to a class that implements that interface? 带接口的 Java 匿名类 - Java Anonymous Class with Interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM