ICloneable 인터페이스는 기존 개체, 즉 클론의 복사본을 만듭니다.
단 하나의 방법만 있습니다 -
-
클론() - 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 = 1695mm Width of car = 1695mm