从父(base)转换为子(派生)类? [英] Cast from parent(base) to son(derived) class?

查看:83
本文介绍了从父(base)转换为子(派生)类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的课程:


公共类BaseClass

{

私人字符串属性A;


受保护的字符串PropertyA

{

get {return propertyA; }

set {propertyA = value; }

}


私有字符串propertyB;


受保护的字符串PropertyB

{

得到{return propertyB; }

set {propertyB = value; }

}

}


现在我有一个派生类:


public class DerivedClass

{

private string propertyC;


protected string PropertyC

{

得到{return propertyC; }

set {propertyC = value; }

}

}


如何创建DerivedClass的构造函数方法,该方法接收

来自基类的对象并将其强制转换为派生类(以便复制

它们共有属性的值)?


我问这是因为这个构造函数显然会引发编译错误:


public DerivedClass(BaseClass oObj)

{

this =(DerivedClass) oObj;

}


除了Reflection之外,有没有办法做到这一点?


提前谢谢。

$ b $bAndrés[knocte]


-

I have a simple class:

public class BaseClass
{
private string propertyA;

protected string PropertyA
{
get { return propertyA; }
set { propertyA = value; }
}

private string propertyB;

protected string PropertyB
{
get { return propertyB; }
set { propertyB = value; }
}
}

Now I have a derived class:

public class DerivedClass
{
private string propertyC;

protected string PropertyC
{
get { return propertyC; }
set { propertyC = value; }
}
}

How can I create a constructor method of DerivedClass that receives an
object from the base class and casts it to derived class (so as to copy
the value of the properties they have in common)?

I ask this because this constructor obviously raises a compile error:

public DerivedClass(BaseClass oObj)
{
this = (DerivedClass)oObj;
}

Is there a method to do this apart from Reflection?

Thanks in advance.

Andrés [ knocte ]

--

推荐答案


$ b $bAndrésG。Aragoneses [knocte]写道:

Andrés G. Aragoneses [ knocte ] wrote:

我有一个简单的课程:


public class BaseClass

{

private string propertyA;


protected string PropertyA

{

得到{归还财产A; }

set {propertyA = value; }

}


私有字符串propertyB;


受保护的字符串PropertyB

{

得到{return propertyB; }

set {propertyB = value; }

}

}


现在我有一个派生类:


public class DerivedClass

{

private string propertyC;


protected string PropertyC

{

得到{return propertyC; }

set {propertyC = value; }

}

}


如何创建DerivedClass的构造函数方法,该方法接收

来自基类的对象并将其强制转换为派生类(以便复制

它们共有属性的值)?
I have a simple class:

public class BaseClass
{
private string propertyA;

protected string PropertyA
{
get { return propertyA; }
set { propertyA = value; }
}

private string propertyB;

protected string PropertyB
{
get { return propertyB; }
set { propertyB = value; }
}
}

Now I have a derived class:

public class DerivedClass
{
private string propertyC;

protected string PropertyC
{
get { return propertyC; }
set { propertyC = value; }
}
}

How can I create a constructor method of DerivedClass that receives an
object from the base class and casts it to derived class (so as to copy
the value of the properties they have in common)?



如果你只需要复制他们共有的属性,你就不需要b $ b需要投射它。


只需使用:


PropertyA = oObj.PropertyA;

PropertyB = oObj.PropertyB;


Jon

If you only need to copy the properties they have in common, you don''t
need to cast it.

Just use:

PropertyA = oObj.PropertyA;
PropertyB = oObj.PropertyB;

Jon


Jon Skeet [C#MVP]escribió:
Jon Skeet [C# MVP] escribió:

如果你只需要复制他们共有的属性,你就不需要b $ b需要施放它。


只需使用:


PropertyA = oObj.PropertyA;

PropertyB = oObj.PropertyB;
If you only need to copy the properties they have in common, you don''t
need to cast it.

Just use:

PropertyA = oObj.PropertyA;
PropertyB = oObj.PropertyB;



是的,但我想避免编码,因为它应该是自动的(因为它是
DerivedClass派生自BaseClass,因此它包含相同的

属性)。


谢谢。

$ b $bAndrés[knocte]


-

Yes, but I want to avoid coding that, because it should be automatic (as
DerivedClass derives from BaseClass and thus it contains the same
properties).

Thanks.

Andrés [ knocte ]

--


你好Andres,


以下代码适用于我...


类父母

{

private int _A;


public int A

{

get {return _A; }

set {_A = value; }

}


private int _B;


public int B

{

得到{return _B; }

set {_B = value; }

}

}


class孩子:父母

{

public Child(父母p)

{

this.A = pA;

this.B = pB;

}


private int _C;


public int C

{

得到{return _C; }

set {_C = value; }

}


}


private void button1_Click(object sender,EventArgs e)

{

父母p =新父母();

pA = 1;

pB = 2;


子c =新子(p);

cC = 3;


MessageBox.Show(cAToString()); < br $>
}


-

干杯,

加里
http://www.garyshort.org/
$ b $bAndrésG。Aragoneses [knocte ]写道:
Hello Andres,

The following code works for me...

class Parent
{
private int _A;

public int A
{
get { return _A; }
set { _A = value; }
}

private int _B;

public int B
{
get { return _B; }
set { _B = value; }
}
}

class Child : Parent
{
public Child(Parent p)
{
this.A = p.A;
this.B = p.B;
}

private int _C;

public int C
{
get { return _C; }
set { _C = value; }
}

}

private void button1_Click(object sender, EventArgs e)
{
Parent p = new Parent();
p.A = 1;
p.B = 2;

Child c = new Child(p);
c.C = 3;

MessageBox.Show(c.A.ToString());
}

--
Cheers,
Gary
http://www.garyshort.org/
Andrés G. Aragoneses [ knocte ] wrote:

我有一个简单的课程:


公共类BaseClass

{

私人字符串属性A;


受保护的字符串PropertyA

{

get {return propertyA; }

set {propertyA = value; }

}


私有字符串propertyB;


受保护的字符串PropertyB

{

得到{return propertyB; }

set {propertyB = value; }

}

}


现在我有一个派生类:


public class DerivedClass

{

private string propertyC;


protected string PropertyC

{

得到{return propertyC; }

set {propertyC = value; }

}

}


如何创建DerivedClass的构造函数方法,该方法接收

来自基类的对象并将其强制转换为派生类(以便复制

它们共有属性的值)?


我问这是因为这个构造函数显然会引发编译错误:


public DerivedClass(BaseClass oObj)

{

this =(DerivedClass) oObj;

}


除了Reflection之外,有没有办法做到这一点?


提前谢谢。

$ b $bAndrés[knocte]


-
I have a simple class:

public class BaseClass
{
private string propertyA;

protected string PropertyA
{
get { return propertyA; }
set { propertyA = value; }
}

private string propertyB;

protected string PropertyB
{
get { return propertyB; }
set { propertyB = value; }
}
}

Now I have a derived class:

public class DerivedClass
{
private string propertyC;

protected string PropertyC
{
get { return propertyC; }
set { propertyC = value; }
}
}

How can I create a constructor method of DerivedClass that receives an
object from the base class and casts it to derived class (so as to copy
the value of the properties they have in common)?

I ask this because this constructor obviously raises a compile error:

public DerivedClass(BaseClass oObj)
{
this = (DerivedClass)oObj;
}

Is there a method to do this apart from Reflection?

Thanks in advance.

Andrés [ knocte ]

--


这篇关于从父(base)转换为子(派生)类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆