我想检测何时将某些项添加到数组列表或从中删除某些项。其实我有一些代码如下:
public class myClass {
MyCustomArrayList<MyObject> al;
public void method1() {
al.add(myObject);
// Do other works
al.remove(myObject)
// Do other works
}
private void DoByEachAdd() {
//I want array list call this method by adding each item to it.
// It should be in this class because it is doing some works
// related to this class. for example changing some private variables
}
private void DoByEachRemove() {
// I want array list call this method by adding each item to it.
// It should be in this class too.
}
}
我知道数组列表不具备监听器或某种通知或事件的能力,如果我想检测添加应该有自定义数组列表。像下面这样的东西:
class MyArrayList<T> {
private ArrayList<T> list;
public MyList(){
list = new ArrayList<>();
...
}
public void add(T t) {
list.add(t) {
//do other things you want to do when items are added
}
public T remove(T t) {
list.remove(t);
//do other things you want to do when items are removed
}
(我是从here获得的)
所以问题是:当MyArrayList
和al
时,如何通知DoByEachAdd
DoByEachRemove
)调用remove
和add
方法的对象已触发{{1}}方法。有些机构有什么想法吗?
答案 0 :(得分:0)
您需要授予myClass
至MyArrayList
class MyArrayList<T> {
private ArrayList<T> list;
private myClass theClass;
public MyList(myClass theClass){
list = new ArrayList<>();
this.theClass = theClass;
...
}
public void add(T t) {
list.add(t) {
//do other things you want to do when items are added
theClass.DoByEachAdd();
}
public T remove(T t) {
list.remove(t);
//do other things you want to do when items are removed
theClass.DoByEachRemove
}
并在myClass
中将对象提供给您的列表
public class myClass {
MyCustomArrayList<MyObject> al;
public myClass(){
al = new MyCustomArrayList<MyObject>(this);
}
public void method1() {
al.add(myObject);
// Do other works
al.remove(myObject)
// Do other works
}
public void DoByEachAdd() {
//I want array list call this method by adding each item to it.
// It should be in this class because it is doing some works
// related to this class. for example changing some private variables
}
public void DoByEachRemove() {
// I want array list call this method by adding each item to it.
// It should be in this class too.
}
}
答案 1 :(得分:0)
您可能需要使用Callback
机制。
Java提供带接口的回调机制 使用以下步骤:
使用抽象方法定义接口 其名称为服务器所知。
每个客户端在执行操作时都要执行操作 服务器事件发生实现接口,从而 为抽象方法提供代码。
当客户端注册服务器时,服务器 拥有一个引用客户端的实例变量 其类型是接口类型。
服务器类通过调用来调用客户端操作 该客户端的接口方法。
请参阅以下代码并根据您的要求进行修改:
该代码用于信用卡应用程序模拟。该要求与您的要求类似,即在调用某种方法时自动触发某些方法。在下面的代码中,pinChanged()
方法会在changePin()
时自动调用方法被称为。
public interface PinChangeListener {
public void pinChanged();
}
public CreditCard {
public PinChangeListener pinChangeListener;
private int pin;
public changePin(int pin) {
this.pin = pin;
if (pinChangeListener != null) {
pinChangeListener.pinChanged();
}
}
}
要将回调/监听器连接到信用卡,您只需要实现PinChangeListener方法:
creditCard.pinChangeListener = new PinChangeListener() {
public void pinChanged() {
System.out.println("The pin has been changed");
}
};
答案 2 :(得分:0)
首先,遵循命名约定。其次,您用于同一个班级的三个班级名称MyList
,MyArrayList
和MyCustomArrayList
会让人感到困惑。至于您的问题,您必须在MyArrayList
类型的myClass
内有一个实例字段(除非您想重构DoByEachAdd
和DoByEachRemove
为static
)。这可以通过将其添加为构造函数参数来完成,例如
// inside MyArrayList
private ArrayList<T> list;
private final myClass instance;
public MyArrayList(myClass instance) { // <-- NOT MyList
list = new ArrayList();
this.myClass = myClass;
}
另外,我质疑你的做法。其他具有MyArrayList
实例的类只能使用add
的{{1}}和remove
方法。如果您想节省很多麻烦并且所有方法都可见,请将ArrayList
声明为list
或将public final
设为MyArrayList
的子类,例如
ArrayList
如果您坚持能够从public class MyArrayList<T> extends ArrayList<T> {
private final myClass instance;
public MyArrayList(myClass instance) { // <-- NOT MyList
list = new ArrayList();
this.myClass = myClass;
}
@Override
public boolean add(T t) {
boolean returnThis = super.add(t);
// do some stuff
instance.DoByEachAdd();
return returnThis;
}
@Override
public boolean remove(T t) {
boolean returnThis = super.remove(t);
// do some stuff
instance.DoByEachRemove();
return returnThis;
}
}
返回T
,请声明另一种方法:
remove