ICloneable接口在C#中做什么?
ICloneable接口创建现有对象的副本,即克隆。
它只有一个方法-
Clone()-该
clone()
方法创建一个新对象,该对象是当前实例的副本。
以下是显示如何使用Icloneable接口执行克隆的示例-
示例
using System;class Car : ICloneable {
int width;
public Car(int width) {
this.width = width;
}
public object Clone() {
return new Car(this.width);
}
public override string ToString() {
return string.Format("Width of car = {0}",this.width);
}
}
class Program {
static void Main() {
Car carOne = new Car(1695);
Car carTwo = carOne.Clone() as Car;
Console.WriteLine("{0}mm", carOne);
Console.WriteLine("{0}mm", carTwo);
}
}
输出结果
Width of car = 1695mmWidth of car = 1695mm
以上是 ICloneable接口在C#中做什么? 的全部内容, 来源链接: utcz.com/z/357239.html