PDA

View Full Version : سوال: اجرا کردن همزمان سازنده های (چند سازنده) یک کلاس در کلاس دیگر



alexmcse
دوشنبه 04 اسفند 1393, 01:32 صبح
یک پروپرتی برای یوزر کنترل
دوستان چند تا کلاس دارم به نام های A,B,C
یک کلاس بنام D داریم که می خواهیم هر یک از 3 کلاس فوق رادر کلاس D به صورت پروپرتی تعریف کنیم
بعد کلاس D را در یک یوزر کنترل استفاده کنیم
مشکل من این است که زمانی که کلاس های A,B,C را در کلاس D مقدار دهی میکنم یک کلاس اجرا میشود

public class A
{
private string a1;
public string A1
{
get { return a1; }
set { a1 = value; }
}
private string a2;
public string A2
{
get { return a2; }
set { a2 = value; }
}
private string a3;
public string A3
{
get { return a3; }
set { a3 = value; }
}
public A()
{


}
public A(string _a1)
{
a1 = _a1;
}
public A(string _a1, string _a2)
{
a1 = _a1;
a2 = _a2;
}
public A(string _a1, string _a2, string _a3)
{
a1 = _a1;
a2 = _a2;
a3 = _a3;


}
}
public class B
{
private string b1;
public string B1
{
get { return b1; }
set { b1 = value; }
}
private string b2;
public string B2
{
get { return b2; }
set { b2 = value; }
}
private string b3;
public string B3
{
get { return b3; }
set { b3 = value; }
}
private string b4;
public string B4
{
get { return b4; }
set { b4 = value; }
}
public B()
{


}
public B(string _b1)
{
b1 = _b1;
}
public B(string _b1, string _b2)
{
b1 = _b1;
b2 = _b2;
}
public B(string _b1, string _b2, string _b3,string _b4)
{
b1 = _b1;
b2 = _b2;
b3 = _b3;
b4 = _b4;
}
}
public class C
{


private string c1;
public string C1
{
get { return c1; }
set { c1 = value; }
}
private string c2;
public string C2
{
get { return c2; }
set { c2 = value; }
}
private string c3;
public string C3
{
get { return c3; }
set { c3 = value; }
}
private string c4;
public string C4
{
get { return c4; }
set { c4 = value; }
}
private string c5;
public string C5
{
get { return c5; }
set { c5 = value; }
}
public C()
{


}
public C(string _c1)
{
c1 = _c1;
}
public C(string _c1, string _c2)
{
c1 = _c1;
c2 = _c2;
}
public C(string _c1, string _c2, string _c3,string _c4, string _c5)
{
c1 = _c1;
c2 = _c2;
c3 = _c3;
c4 = _c4;
c5 = _c5;
}


}
public class D
{
private A ap;
[TypeConverter(typeof(ExpandableObjectConverter))]
[Editor(typeof(MyEditor), typeof(UITypeEditor))]




public A Ap
{
get
{
return ap;
}
set { ap = value; }
}
private B bp;
[TypeConverter(typeof(ExpandableObjectConverter))]
[Editor(typeof(MyEditor), typeof(UITypeEditor))]
public B Bp
{
get { return bp; }
set { bp = value; }
}
private C cp;
[TypeConverter(typeof(ExpandableObjectConverter))]
[Editor(typeof(MyEditor), typeof(UITypeEditor))]
public C Cp
{
get { return cp; }
set { cp = value; }
}
//این سه سازنده همزمان باید اجراشوند
//اما مشکل این است که یکی از این سازندها اجرا میشوند


public D(string a,string b,string p,string w,string t)
{
cp = new C(a, b, p, w, t);//یا
}
public D(string h, string g, string b)

{
ap = new A(h, g, b);//یا
}
public D(string m, string f, string n, string s)
{
bp = new B(m, f, n, s);//یا


}


}
[Serializable]
public class UC : UserControl
{
private D dp;
[TypeConverter(typeof(ExpandableObjectConverter))]
[Editor(typeof(MyEditor), typeof(UITypeEditor))]
public D Dp
{
get { return dp; }
set { dp = value; }
}
public UC ()
{//فقط این سازنده مقدار دهی میشود
dp = new D("hello", "good buy", "tanku");
//*********************************************
//دو سازنده زیر مقدار دهی نمیشوند
System.Threading.Thread.Sleep(1000);
ParallelQuery p;

dp = new D("example1", "example2", "example3");
System.Threading.Thread.Sleep(1000);
dp = new D("student1", "student2", "student3");
}
}

تشکر

plus
دوشنبه 04 اسفند 1393, 02:16 صبح
قانون زبان این هست که با ایجاد یک شئ، فقط و فقط یکی از سازنده ها اجرا میشه؛ البته در اون سازنده شما میتونید سازنده دیگری رو فراخوانی کنید ولی روش معمولی نیست.

class Heater {
private int degree;
private bool turnedOn;
public Heater(int degree) : this(degree, true ) {
}
public Heater(int degree, bool turnedOn) {
this.degree = degree;
this.turnedOn = turnedOn;
}
}
..
..
Heater heater = new Heater(25);

در این حالت سازنده اول دومی رو که دو آرگومان داره صدا میزنه و مقدار پیشفرض true رو برای آرگومان دوم به اون ارسال میکنه.