gpt4 book ai didi

java - 扩展 ArrayList 以支持事件

转载 作者:行者123 更新时间:2023-11-29 05:58:51 25 4
gpt4 key购买 nike

我正在尝试扩展 Java ArrayList 以支持此示例中描述的事件: http://www.exampledepot.com/egs/java.util/CustEvent.html

问题是 java 在以下行中报告“无法对非静态类型 MyEvent 进行静态引用”:

public interface MyEventListener extends EventListener {
public void myEventOccurred(MyEvent evt);
}

我认为这与泛型有关,但我真的不知道如何解决这个问题。有人可以帮助我吗?

这是我正在尝试做的事情的完整来源:

public class ArrayList<E> extends java.util.ArrayList<E> {

private static final long serialVersionUID = 1L;

// Declare the event. It must extend EventObject.
public class MyEvent extends EventObject {

private static final long serialVersionUID = 1L;

public MyEvent(Object source) {

super(source);
}
}

// Declare the listener class. It must extend EventListener.
// A class must implement this interface to get MyEvents.
public interface MyEventListener extends EventListener {
public void myEventOccurred(MyEvent evt); //Error occurs here
}
}

最佳答案

正如其他用户所指出的,您可以在他们自己的文件中执行所有类和接口(interface),这样您就不会有问题 - 这是一种更好的方法。如果您打算使用内部类和接口(interface),那么您需要将它们声明为static:

public class ArrayList<E> extends java.util.ArrayList<E> {

private static final long serialVersionUID = 1L;

// Declare the event. It must extend EventObject.
public static class MyEvent extends EventObject {

private static final long serialVersionUID = 1L;

public MyEvent(Object source) {

super(source);
}
}

// Declare the listener class. It must extend EventListener.
// A class must implement this interface to get MyEvents.
public static interface MyEventListener extends EventListener {
public void myEventOccurred(MyEvent evt); //Error occurs here
}
}

关于java - 扩展 ArrayList 以支持事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10970411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com