大神,下面这道题怎么做....浅度复制和深度复制的区别?有下列Car类和Tyre类,实现ICloneable接口的Clone方法,完成Car类的深度复制public class Car:ICloneable
{
public double Speed { get; set; }
public string Color{ get; set; }
public Tyre CarTyre{ get; set; } public object Clone()
{
//填入代码
}
} public class Tyre
{
public string Type { get; set; }
public double Size { get; set; } public Tyre (string type, double size)
{
Type = type;
Size = size;
}
}
{
public double Speed { get; set; }
public string Color{ get; set; }
public Tyre CarTyre{ get; set; } public object Clone()
{
//填入代码
}
} public class Tyre
{
public string Type { get; set; }
public double Size { get; set; } public Tyre (string type, double size)
{
Type = type;
Size = size;
}
}
{
object ICloneable.Clone()
{
return new Car{Tyre =new Tyre{ Type =this.CarTyre.Type,
Size = this.CarTyre.Size;}
}
public Car Clone()
{
return ((ICloneable)this).Clone() as Car;
}
}
ps:建议这里显示实现接口而不是直接实现接口,同时提供一个更为友好的方法供外边使用(public Car Clone())
{
return new Car{Speed=this.Speed,Color=this.Color,CarTyre=new Tyre(this.CarTyre.Type,this.CarTyre.Size)};
}