如何快速的复制父类的数据?
public class A
{
string data1 = "";
public string Data1
{
get { return data1; }
set { data1 = value; }
}
private int data2 = "";
public int Data2
{
get { return data2; }
set { data2 = value; }
}
}
public class B:A
{
public B(A a)
{
// 有没有办法在这里快速复制?
this.Data1 = a.Data1;
this.Data2 = a.Data2;
}
private string data3 = "";
public string Data3
{
get { return data3; }
set { data3 = value; }
}
}
------解决方案--------------------public class A
{
string data1 = "";
public A(A a){
data1=a.data1;
data2= a.data2;
}
public string Data1
{
get { return data1; }
set { data1 = value; }
}
private int data2 = "";
public int Data2
{
get { return data2; }
set { data2 = value; }
}
}
public class B:A
{
public B(A a) :base(a)
{
}
private string data3 = "";
public string Data3
{
get { return data3; }
set { data3 = value; }
}
}
------解决方案--------------------其实父类子类的,有些误导。这样说,
1、如果是实例的复制,拷贝是免不了的。
2、如果只是要用“父类”的数据,这不是继承,而要用包含。
C# code
public B
{
A a; // B包含A
public B(A a)
{
this.a = a;
}
}