本文介绍了如何在派生类中隔离事件生成代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 嘿所有, 当我的缓冲区不是空的时候,我的一类需要告诉外面的世界。问题是C#似乎迫使你把 事件提升代码放在基类中。为了说明,考虑一下我用Java做什么: 公共接口DataAvailabilityListener扩展java.util.EventListener { void dataArrived(DataAvailabilityEvent event); } 然后,在我的基类中,我可以这样做: 公共抽象类SmartQueue { addDataAvailabilityListener(DataAvailabilityListen er listener); } 然后在我的实现类中,说一个内存支持的智能队列,我可以这样做:这样做: 公共类MemorySmartQueue实现SmartQueue { public void poolForData (){ //数据可用! while(iter.hasNext()){ DataAvailabilityListener lis = (DataAvailabilityListener)iter.next(); lis.dataArrived(someEvent); } } } 简单而直接。但是,考虑一下C#实现:在 界面中,我可能有这样的东西: 公共抽象类SmartQueue { 公共事件DataAvailabilityEventHandler DataArrived; } 现在,考虑一下我在MemorySmartQueue中要做的事情: 公共类MemorySmartQueue:SmartQueue { public override void poolForData(){ //数据可用! if(DataArrived == null){ // BZZZZTTT !!!只能在SmartQueue中执行此操作! DataArrived(this,args); } } } 我不能相信这一点。要么我错过了一个非常明显的事情,要么我必须处理这个......这......尴尬的机制。为什么地狱 难道它不允许我在派生类中举起一个事件?我不希望 将任何行为放在我的抽象类中,我想只放一个界面! 有什么方法可以解决这个问题吗? (另外我希望惠德贝会给我们一套 的收藏,该死的)。 TIA! 长老 解决方案 java.util.EventListener { 接口! Hey all,A class of mine needs to tell the outside world when its buffer is notempty. The problem is that C# seems to force you to put theevent-raising code in the base class. To illustrate, consider what I''lldo in Java:public interface DataAvailabilityListener extends java.util.EventListener {void dataArrived(DataAvailabilityEvent event);}then, in my base class, I can do this:public abstract class SmartQueue {addDataAvailabilityListener(DataAvailabilityListen er listener);}then in my implementation class, say a memory-backed smart queue, I cando this:public class MemorySmartQueue implements SmartQueue {public void poolForData() {// data available!while(iter.hasNext()) {DataAvailabilityListener lis =(DataAvailabilityListener)iter.next();lis.dataArrived(someEvent);}}}Simple and straighforward. However, consider a C# implementation: in theinterface, I may have something like this:public abstract class SmartQueue {public event DataAvailabilityEventHandler DataArrived;}Now, consider what I have to do in MemorySmartQueue:public class MemorySmartQueue : SmartQueue {public override void poolForData() {// data available!if(DataArrived == null) {// BZZZZTTT!!! Can only do this in SmartQueue!DataArrived(this, args);}}}I can''t believe this. Either I''m missing a really obvious thing, or Ihave to deal with this... this... awkward mechanism. Why the helldoesn''t it allow me to raise an event in the derived class? I don''t wantto put any behaviour in my abstract class, I want to put just an interface!Is there any way around this? (Plus I hope Whidbey will give us a Setcollection, dammit).TIA!Elder 解决方案java.util.EventListener { interface! 这篇关于如何在派生类中隔离事件生成代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 09:33