java 当一个类也扩展了另一个类时,如何让它扩展 Observable?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1658702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make a Class extend Observable when it has extended another class too?
提问by unj2
I am learning Java and I want to make my class into an observable class.
我正在学习 Java,我想把我的班级变成一个可观察的班级。
However I already have it extending another class.
但是我已经有了它扩展另一个类。
What should I do?
我该怎么办?
回答by Adamski
I'd recommend avoiding using the Observableclass altogether, but rather define event-specific listeners and corresponding event definitions. Then define a list of listeners within your class along with methods to add and remove listeners, and propagate events to them (see below).
我建议Observable完全避免使用该类,而是定义特定于事件的侦听器和相应的事件定义。然后在您的类中定义一个侦听器列表以及添加和删除侦听器的方法,并将事件传播给它们(见下文)。
Observableforces you to use java.lang.Objectto represent events and then check the event type using instanceof, which is an ugly non-OO approach, and makes the code more difficult to understand. If you look at the classes within the javax.swing package you'll see they avoided using Observer/ Observablealtogether and used an approach similar to the below.
Observable强制您使用java.lang.Object来表示事件,然后使用来检查事件类型instanceof,这是一种丑陋的非 OO 方法,并使代码更难以理解。如果您查看 javax.swing 包中的类,您会发现它们完全避免使用Observer/Observable并使用类似于下面的方法。
Event Definition
事件定义
public class MyChangeEvent extends EventObject {
// This event definition is stateless but you could always
// add other information here.
public MyChangeEvent(Object source) {
super(source);
}
}
Listener Definition
听者定义
public interface MyChangeListener {
public void changeEventReceived(MyChangeEvent evt);
}
Class Definition
类定义
public class MyClass {
// Use CopyOnWriteArrayList to avoid ConcurrentModificationExceptions if a
// listener attempts to remove itself during event notification.
private final CopyOnWriteArrayList<MyChangeListener> listeners;
public class MyClass() {
this.listeners = new CopyOnWriteArrayList<MyChangeListener>();
}
public void addMyChangeListener(MyChangeListener l) {
this.listeners.add(l);
}
public void removeMyChangeListener(MyChangeListener l) {
this.listeners.remove(l);
}
// Event firing method. Called internally by other class methods.
protected void fireChangeEvent() {
MyChangeEvent evt = new MyChangeEvent(this);
for (MyChangeListener l : listeners) {
l.changeEventReceived(evt);
}
}
}
回答by Hyman
Java doesn't allow multiple inheritance, so there is no direct way to do it. You should consider using a delegate pattern having your main object that delegates his observer behaviour to an another object..
Java 不允许多重继承,因此没有直接的方法可以做到。您应该考虑使用委托模式,让您的主要对象将他的观察者行为委托给另一个对象。
class YourObject extends ItsAncestorClass
{
private Observer includedObserver;
public Observer getHisObserver(..)
}
Another approach would be turning the object from which your class is extending to an interface, then you'll be allowed to extend from Observer.
另一种方法是将您的类从其扩展的对象转换为接口,然后您将被允许从 Observer 扩展。
回答by Chuck Deal
Another option is to wrap your object in an Observable object.
另一种选择是将您的对象包装在一个 Observable 对象中。
public class MyObjectObservableWrapper implements Observable {
private MyObject myObject;
public MyObjectObservaleWrapper(MyObject myObject){
this.myObject = myObject;
}
// interface methods here
}
This option works when the data to be used by the Observable methods is accessible through public methods of MyObject. So, it may not be suitable for all cases.
当 Observable 方法使用的数据可通过 MyObject 的公共方法访问时,此选项有效。因此,它可能不适用于所有情况。
回答by harichetlur
You can extend Observable and wrap the original parent in your to-be observable child class. Delegate all method calls to the wrapped object.
您可以扩展 Observable 并将原始父类包装在您要观察的子类中。将所有方法调用委托给包装对象。
回答by sinuhepop
Multiple inheritance (to extend two classes) is not possible in Java. Is considered a bad design in almost all cases.
多重继承(扩展两个类)在 Java 中是不可能的。在几乎所有情况下都被认为是一个糟糕的设计。
If you give us some more information, maybe somebody can help you a little more.
如果您给我们提供更多信息,也许有人可以帮助您更多。
回答by Joshua
Use a bridge.
使用桥梁。
Create a class that extends Observable that the first class just calls the methods of the second class.
创建一个扩展 Observable 的类,第一个类只调用第二个类的方法。
Bridge method detail:
桥接方法详解:
public class XXX {
public class XXXObservableBridge : Observable {
public void RaiseEvent();
// Listeners etc
}
private XXXObservableBridge ObservableBridge;
XXX() {
ObservableBridge = new ObservableBridge;
}
public Observable AsObservable() { return ObservableBidge; }
public void RaiseEvent() { ObservableBridge.RaiseEvent(); }
}

