Java-创建自定义事件和侦听器 [英] Java - Create a Custom event and listener

查看:49
本文介绍了Java-创建自定义事件和侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Java中创建自定义事件和侦听器.我已经看过这些文章和问题了:

I am trying to make a custom event and listener in Java. I already looked at these articles and questions:

在Java中创建自定义事件

Java自定义事件处理程序和侦听器

https://www.javaworld.com/article/2077333/core-java/mr-happy-object-teaches-custom-events.html

但是我仍然无法真正绕过它.这是我想做的:

But I still can't really wrap my head around it. This is what I want done:

我有一个 String 对象,该对象的内容随程序运行而变化.我希望能够向该字符串添加一个侦听器,以侦听它是否包含特定字符串以及何时运行一段代码.我想这样使用它:

I have a String object whose content changes as the program runs. I want to be able to add a listener to the string which listens wether it contains a specific string and when it does run a piece of code. I want to use it like this:

String string = "";
//String.addListener() and textListener need to be created
string.addListener(new textListener("hello world") {
    @Override
    public void onMatch(
         System.out.println("Hello world detected");
    )
}

//do a bunch of stuff

string = "The text Hello World is used by programmers a lot"; //the string contains "Hello World", so the listener will now print out "Hello world detected"

我知道可能会有更简单的方法来做,但是我想知道如何以这种方式做.

I know there might be easier ways of doing this, but I would like to know how to do it this way.

感谢@Marcos Vasconcelos指出您无法向 String 对象添加方法,所以有没有办法使用@Ben这样的自定义类?

Thank you @Marcos Vasconcelos for pointing out that you cannot add methods to a String object, so is there a way I can use a custom class like @Ben pointed out?

推荐答案

所以我举了一个最小的示例,它可能会对您有所帮助:

So I made a minimal example maybe that will help you:

您需要一个用于收听者的界面:

You need an interface for your listener:

public interface MyEventListener
{
    public void onMyEvent();
}

然后对于您的String,您需要一些包装类来处理您的事件

Then for your String you need some wrapper class that also handles your events

public class EventString
{
    private String                  myString;

    private List<MyEventListener>   eventListeners;

    public EventString(String myString)
    {
        this.myString = myString;
        this.eventListeners = new ArrayList<MyEventListener>();
    }

    public void addMyEventListener(MyEventListener evtListener)
    {
        this.eventListeners.add(evtListener);
    }

    public void setValue(String val)
    {
        myString = val;

        if (val.equals("hello world"))
        {
            eventListeners.forEach((el) -> el.onMyEvent());
        }
    }
}

您会看到 myString 字段是私有字段,只能使用 setValue 方法访问.这样我们就可以看到事件条件何时触发.

You see that the myString field is private and only accessible using the setValue method. This is so we can see when our event condition triggers.

然后您只需要执行一些实现,例如:

And then you only need some implementation of this, such as:

EventString temp = new EventString("test");

temp.addMyEventListener(() -> {
    System.out.println("hello world detected");
});

temp.setValue("hello world");

这篇关于Java-创建自定义事件和侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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