class Person {
public string Name { get; set; }
public int Age { get; set;}
//Parameterized Constructor
public Person(string name, int age) {
Name = name;
Age = age;
}
}
class Person {
public string Name { get; set; }
public int Age { get; set; }
//copy constructor
public Person(Person other) {
Name = other.Name;
Age = other.Age;
}
}
ตัวอย่าง:
// C# Program to illustrate the use
// of Copy constructor
using System;
namespace copyConstructorExample {
class Vehicle {
// variables
private string name;
private string color;
private int quantity;
// Copy constructor
public Vehicle(Vehicle a)
{
name = a.name;
color = a.color;
quantity = a.quantity;
}
// Parameterized constructor
public Vehicle(string name, string color, int quantity)
{
this.name = name;
this.color = color;
this.quantity = quantity;
}
// Get details of Vehicles
public string DetailsofVehicle{
get{
return "Type: " + name.ToString() +
"\nColor: " + color.ToString() +
"\nQuantity: " + quantity.ToString();
}
}
// Main Method
public static void Main(){
// Create a new object.
Vehicle v1 = new Vehicle("Bike", "Black", 40);
// here is v1 details are copied to v2.
Vehicle v2 = new Vehicle(v1);
Console.WriteLine(v2.DetailsofVehicle);
}
}
}
เหมาะสำหรับวัตถุที่ไม่ซับซ้อน หรือเมื่อต้องการให้วัตถุที่คัดลอกมาอ้างอิงข้อมูลเดียวกับวัตถุต้นฉบับ เร็วกว่า deep copy เนื่องจากไม่มีการจัดสรรหน่วยความจําใหม่
class ShallowCopy : ICloneable
{
public int I {get;set;}
public int J {get;set;}
//method for cloning object
public object Clone()
{
return this.MemberwiseClone();
}
}
class Demo
{
public static void Main()
{
ShallowCopy obj=new ShallowCopy();
Console.WriteLine(“--------before Shellow Clopy------”);
ShallowCopy objClone=obj;
obj.I=10;// setting obj value after cloning..
Console.WriteLine(“objvalue : {0} \t Clone value : {1}”,obj.I,objClone.I=10);
Console.WriteLine(“--------after Shellow Copy------”);
ShallowCopy objClone2=(ShallowCopy)obj.Clone(); // cast object to //ShallowCopy
obj.I=1000; // MemberwiseClone() will not use this reference..
Console.WriteLine(“after using MemberwiseClone() Clone() method :{0}”,objClone2.I);
}
}
ผลลัพธ์
--------before Shellow Clopy------
objvalue : 0 Clone value : 0
--------after Shellow Copy------
after using MemberwiseClone() Clone() method : 10
class ReferenceType
{
public int RFT { get; set; }
}
class ShallowCopy : ICloneable
{
public int I { get; set; }
public int J { get; set; }
public ReferenceType K = new ReferenceType();
//Method updated for reference type ..
public object Clone()
{
// Shalllow Copy..
ShallowCopy SC = (ShallowCopy)this.MemberwiseClone();
// Deep copy...
ReferenceType RT = new ReferenceType();
RT.RFT = this.K.RFT;
SC.K = RT;
return SC;
}
public static void Main(String[] args)
{
ShallowCopy obj = new ShallowCopy();
obj.K.RFT = 100;
ShallowCopy objclone = (ShallowCopy)obj.Clone();
obj.K.RFT = 200; // make changes in obj.
Console.WriteLine(objclone.K.RFT);
}
}
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person(Person other) // Copy constructor
{
Name = other.Name;
Age = other.Age;
}
}
class ABC
{
// Instance variable of the class ABC
int x = 30;
}
public class ShallowCopyExample
{
// Main method
public static void main(String argvs[])
{
// Creating an object of the class ABC
ABC obj1 = new ABC();
// It will copy the reference, not the value
ABC obj2 = obj1;
// Updating the value to 6 using the reference variable obj2
obj2.x = 6;
// Printing the value of x using reference variable obj1
System.out.println("The value of x is: " + obj1.x);
}
}
class ABC
{
// Instance variable of the class ABC
int x = 30;
}
public class DeepCopyExample
{
// Main method
public static void main(String argvs[])
{
// Creating an object of the class ABC
ABC obj1 = new ABC();
// Creating another object of the class ABC for deep copy
ABC obj2 = new ABC();
// Updating the value to 6 using the reference variable obj2
obj2.x = 6;
// Printing the value of x using reference variable obj1
System.out.println("The value of x is: " + obj1.x);
}
}
ผลลัพธ์
The value of x is: 30
ตัวอย่าง Java เทียบ copy constructor กับ C#
C#:
class Person {
public string Name { get; set; }
public int Age { get; set; }
// Copy Constructor
public Person(Person other) {
Name = other.Name;
Age = other.Age;
}
}
Java:
class Person {
String name;
int age;
// Copy Constructor
public Person(Person other) {
this.name = other.name;
this.age = other.age;
}
}
import copy
class DeepCopyExample:
def __init__(self, data):
self.data = data
def __str__(self):
return f"Original Object: {self.data}"
def deep_copy(self):
return copy.deepcopy(self)
# Example usage
original_object = DeepCopyExample([1, [2, 3], 4])
copy_object = original_object.deep_copy()
print(original_object)
print(copy_object)
# Modify the copied object
copy_object.data[1].append(99)
# Changes not reflected in the original object
print(original_object)
print(copy_object)
ผลลัพธ์
Original Object: [1, [2, 3], 4]
Original Object: [1, [2, 3], 4]
Original Object: [1, [2, 3], 4]
Original Object: [1, [2, 3, 99], 4]
ตัวอย่าง Python เทียบ copy constructor กับ C#
C#:
class Person {
public string Name { get; set; }
public int Age { get; set; }
// Copy Constructor
public Person(Person other) {
Name = other.Name;
Age = other.Age;
}
}
Person person1 = new Person { Name = "Alice", Age = 30 };
Person person2 = new Person(person1); // Copy constructor
Python:
import copy
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
# Shallow Copy
person1 = Person("Alice", 30)
person2 = copy.copy(person1) # Shallow copy
# Deep Copy
person3 = copy.deepcopy(person1) # Deep copy