|
Listing 10-8 extracted from chapter The .NET 2 type system from a C#2 point of view
Listing 10-7< > Listing 10-9
This listing can be compiled with the command line: csc.exe /target:exe Example_10_8.cs Errors: 0 Warnings: 0
Example_10_8.cs
class Article : System.ICloneable {
public string Description;
public int Price;
public object Clone() {
return this.MemberwiseClone();
}
}
class Order {
public int Quantity;
public Article Article;
public override string ToString() {
return "Order: " + Quantity + " x " + Article.Description +
" Total cost: " + Article.Price * Quantity;
}
// Default constructor.
public Order() { }
// Copy constructor (parameterized).
public Order( Order original , bool bDeepCopy) {
this.Quantity = original.Quantity;
if( bDeepCopy )
this.Article = original.Article.Clone() as Article;
else
this.Article = original.Article;
}
}
class Program {
static void Main() {
Order order = new Order();
order.Quantity = 2;
order.Article = new Article();
order.Article.Description = "Shoes";
order.Article.Price = 80;
System.Console.WriteLine(order);
Order orderClone = new Order( order , true );
orderClone.Article.Description = "Shirt";
System.Console.WriteLine(order);
}
}
Copyright Patrick Smacchia 2006 2007
|