prince-of-persia
دوشنبه 22 مهر 1387, 22:43 عصر
سلام
من می خوام یک کلاس بنویسک که یک event ساده دارد
در واقع کلاس calls1 یک تابع به نام add دارد که اگر مجموع 2 عدد بزرگتر از 12 شد یک event رخ می دهد .
من کد رو توی c# دارم
public class Class1
{
public event EventHandler onadd=null;
public Class1()
{
//
// TODO: Add constructor logic here
//
}
public int add(int a, int b)
{
int c = a + b;
if (c > 12 && onadd !=null)
{
onadd(this, EventArgs.Empty);
}
return c;
}
}
و اما در برنامه اصلی به این شکل استفاده می شود
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Class1 c = new Class1();
c.onadd += new EventHandler(c_onadd);
c.add(12, 14);
}
void c_onadd(object sender, EventArgs e)
{
throw new Exception("The method or operation is not implemented.");
}
}
}
ممنون میشم اگر راهنمایی کنید که نوشتن event برای یک کلاس در جاوا به چه شکل است ؟
یا به معنای دیگه کد بالا رو باز نویسی کنید .
با تشکر
من می خوام یک کلاس بنویسک که یک event ساده دارد
در واقع کلاس calls1 یک تابع به نام add دارد که اگر مجموع 2 عدد بزرگتر از 12 شد یک event رخ می دهد .
من کد رو توی c# دارم
public class Class1
{
public event EventHandler onadd=null;
public Class1()
{
//
// TODO: Add constructor logic here
//
}
public int add(int a, int b)
{
int c = a + b;
if (c > 12 && onadd !=null)
{
onadd(this, EventArgs.Empty);
}
return c;
}
}
و اما در برنامه اصلی به این شکل استفاده می شود
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Class1 c = new Class1();
c.onadd += new EventHandler(c_onadd);
c.add(12, 14);
}
void c_onadd(object sender, EventArgs e)
{
throw new Exception("The method or operation is not implemented.");
}
}
}
ممنون میشم اگر راهنمایی کنید که نوشتن event برای یک کلاس در جاوا به چه شکل است ؟
یا به معنای دیگه کد بالا رو باز نویسی کنید .
با تشکر