当我在 C# 中编程时,我经常使用事件。现在我想在 Java 中实现类似的东西。我知道这两种语言是不同的,并且在实现这种机制方面存在一些差异。好吧,为了提供更好的图片,也许我会描述一下我想要得到的东西,因为我可能会混淆一些术语:
MyClass包含处理启动MyEvent的代码和在某些情况下启动该事件的一些代码。
MyEventArgs是从MyClass传输数据并与事件启动一起发送的类,因此处理MyEvent的函数具有有关MyClass实例状态的一些附加信息。
并且有一个MyApp类,在 main 方法中包含MyClass的实例和处理MyEvent的代码,当在MyClass内引发事件时,某些操作在侦听MyEvent的代码中执行。
如果仍然不清楚,我的意思是实现就像按钮单击背后的 c# 机制一样,只是没有按钮,而是有我的类,有我的事件参数而不是鼠标事件参数,并且有我设计的行为而不是单击.
我试图用谷歌搜索我的问题的一些答案,例如我发现了以下网站:
http://javarevisited.blogspot.com/2011/12/observer-design-pattern-java-example.html http://www.javaworld.com/javaworld/javaqa/2002-03/01-qa-0315-happyevent .html
或者我只是迷路了,我正在寻找不好的地方/使用错误的关键字,或者更有可能我无法理解这些示例中的任何内容/我无法将它们转换为我需要的工作方式。
我要求的是一些示例,或者至少是一些使用MyClass、 MyEvent、 MyEventArgs、名称、指向代码来处理这种机制的代码草案,我应该在我的类中使用来引发事件以及类的一些示例使用和事件处理在 Main 方法中,所以它可以帮助我解决这个问题。
==========编辑========
也许有一些类似的东西可供android开发人员使用?因为我的目标是在我尘封 java 之后进入移动应用程序开发。
==========编辑========
如果有人仍然感兴趣,这里是一些示例代码,它不使用我提到的名称,只是一般显示我在寻找什么:
并且没有确切的解决方案,也许有人会提出类似于我正在寻找的东西的建议?
frmTest.cs
namespace SampleApp
{
    public partial class frmTest : Form
    {
        CountdownTimer ctmrTest;
        public frmTest()
        {
            InitializeComponent();
            ctmrTest = new CountdownTimer(100);
            ctmrTest.CountdownTimerTick += new CountdownTimer.CountdownTimerTickEventHandler(ctmrTest_CountdownTimerTick); 
        }
        void ctmrTest_CountdownTimerTick(object sender, CountdownTimerEventArgs ea)
        {
            lblTimer.Text = ea.timeString;
            if (ea.countdownFinished) countdownEnd();
        }
        private void btnStart_Click(object sender, EventArgs e)
        {
            ctmrTest.Reset();
            ctmrTest.Start();
        }
        void countdownEnd()
        {
            MessageBox.Show("Finish");
        }
    }
}
倒计时计时器.cs
namespace SampleApp
{
    public class CountdownTimer
    {
        Timer tmrTicks = new Timer();
        int secondsLeft = 0;
        int numberOfSecondsToCountdown = 0;
        public bool IsWorking
        {
            get { return tmrTicks.Enabled; }
        }
        public CountdownTimer(int seconds)
        {
            ...
        }
        void tmrTicks_Tick(object sender, EventArgs e)
        {
            ...
            WhenCountdownTimerTick(new CountdownTimerEventArgs(secondsLeft, numberOfSecondsToCountdown, true));
        }
        public void Reset()
        {
            ...
            WhenCountdownTimerTick(new CountdownTimerEventArgs(secondsLeft, numberOfSecondsToCountdown, false));
        }
        public void Stop()
        {
            tmrTicks.Enabled = false;
        }
        public void Start()
        {
            ,,,
        }
        public delegate void CountdownTimerTickEventHandler(object sender, CountdownTimerEventArgs ea);
        public event CountdownTimerTickEventHandler CountdownTimerTick;
        protected virtual void WhenCountdownTimerTick(CountdownTimerEventArgs ea)
        {
            if (CountdownTimerTick != null)
            {
                CountdownTimerTick(this, ea);
            }
        }
    }
}
CountdownTimerEventArgs.cs
namespace SampleApp
{
    public class CountdownTimerEventArgs : EventArgs
    {
        public string timeString = "";
        public bool countdownFinished = false;
        public CountdownTimerEventArgs(int secondsLeft, int SecondsToCountdown, bool isfinished)
        {
            countdownFinished = isfinished;
            timeString = string.Format("{0:00}:{1:00}", secondsLeft / 60, secondsLeft % 60);
        }
    }
}