读书人

小弟刚学习C#请各们高手帮小弟做这一题

发布时间: 2012-01-13 22:43:30 作者: rapoo

小弟刚学习C#,请各们高手帮小弟做这一题!
为Car类编写个构造函数,它接受另一个Car对象作为参数,并复制这个参数的所有属性!

[解决办法]
/// <summary>
/// Car の概要の明です
/// </summary>
public class Car
{
private System.Drawing.Color color;
private int height;
private int width;

public Car(Car car)
{
//
// TODO: コンストラクタ ロジックをここに追加します
//
this.color = car.color;
this.height = car.height;
this.width = car.width;
}
}
[解决办法]
//复制属性和字段
public class car
{
public string aa = null;
car(car c)
{
foreach (System.Reflection.FieldInfo fi in typeof(car).GetFields())
{
fi.SetValue(this,fi.GetValue(c));
}

foreach (System.Reflection.PropertyInfo fi in typeof(car).GetProperties())
{
fi.SetValue(this,fi.GetValue(c));
}
}
}

读书人网 >C#

热点推荐