Java Listener继承 [英] Java Listener inheritance
问题描述
public class AEvent extends EventObject {
...
}
public interface AListener extends EventListener {
public void event1(AEvent event);
}
public class A {
public synchronized void addAListener(AListener l){
..
}
public synchronized void removeAListener(AListener l){
..
}
protected void fireAlistenerEvent1(AEvent event){
..
}
}
一切正常,但我想创建一个A的新子类(称为B),这可能会触发一个新的事件。我正在考虑以下修改:
public class BEvent extends AEvent {
...
public interface BListener extends AListener {
public void event2(BEvent event);
}
public class B extends A {
public synchronized void addBListener(BListener l){
..
}
public synchronized void removeBListener(BListener l){
..
}
protected void fireBListenerEvent2(AEvent event){
..
}
}
这是正确的方法吗?我正在网路上搜索示例,但找不到任何内容。
这个解决方案中有一些我不喜欢的东西:
-
BListener
有两种方法,一种使用AEvent
其他用途Bevent
作为参数。 -
B
class都有addAListener
和addBListener
方法。我应该用private关键字隐藏addAListener吗? [更新:无法隐藏私人关键字] - 与
fireAlistenerEvent1
和fireBListenerEvent1
方法。
我使用的是Java版本1.5。
我不明白为什么 BListener
应该扩展 AListener
。
你是否真的想强迫大家对 B
事件感兴趣实现 event1()
?
另外你不能添加 addAListener() code>,因为派生类不能减少父类中存在的方法的可见性。此外,您不应该需要,否则您将违反 Liskov替代原则(每B必须可以做一个A可以做的事情)。
作为最后一句话,我会使 fire *()
方法受保护。通常没有理由保持公开,减少公共成员的数量使您的公共界面清洁。
I have a java class which fires custom java events. The structure of the code is the following:
public class AEvent extends EventObject {
...
}
public interface AListener extends EventListener {
public void event1(AEvent event);
}
public class A {
public synchronized void addAListener(AListener l) {
..
}
public synchronized void removeAListener(AListener l) {
..
}
protected void fireAListenerEvent1(AEvent event) {
..
}
}
Everything works correctly, but I'd like to create a new subclass of A (call it B), which may fire a new event. I'm thinking of the following modification:
public class BEvent extends AEvent {
...
}
public interface BListener extends AListener {
public void event2(BEvent event);
}
public class B extends A {
public synchronized void addBListener(BListener l) {
..
}
public synchronized void removeBListener(BListener l) {
..
}
protected void fireBListenerEvent2(AEvent event) {
..
}
}
Is this the correct approach? I was searching the web for examples, but couldn't find any.
There are a few things I don't like in this solution:
BListener
has two methods one usesAEvent
the other usesBEvent
as a parameter.B
class both hasaddAListener
andaddBListener
methods. Should I hide addAListener with private keyword? [UPDATE: it's not possible to hide with private keyword]- Similar problem with
fireAListenerEvent1
andfireBListenerEvent1
methods.
I'm using Java version 1.5.
I don't see a reason why BListener
should extend AListener
.
Do you really want to force everyone interested in B
events to also implement event1()
?
Also you can't add addAListener()
, since a derived class can not reduce the visibility of a method that's present in the parent class. Also, you shouldn't need to, or you would violate the Liskov substitution principle (every B must be able to do everything an A can do).
And as a last remark, I'd make the fire*()
methods protected. There's usually no reason at all to keep them public and reducing the number of public members keeps your public interface clean.
这篇关于Java Listener继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!