再论堆栈 3 ——编程的物质基础!【转】【译】

原文地址:http://www.c-sharpcorner.com/UploadFile/rmcochran/chsarp_memory401152006094206AM/chsarp_memory4.aspx

 

A Copy Is Not A Copy

为了清楚的说明这个问题,我们来校验以下两种情形将会发生什么情况:位于堆里面的一个值类型和位于堆里面的一个引用类型。我们先看值类型。看下面的class和struct。我们有一个Dude类,该类包含一个Name元素和两个Shoe(s)。同时,添加一个CopyDude()方法来更加方便的new Dudes.

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public struct Shoe{
 
      public string Color;
  }
 
  public class Dude
 
  {
       public string Name;
 
       public Shoe RightShoe;
 
       public Shoe LeftShoe;
 
       public Dude CopyDude()
 
       {
 
           Dude newPerson = new Dude();
 
            newPerson.Name = Name;
 
            newPerson.LeftShoe = LeftShoe;
 
            newPerson.RightShoe = RightShoe;
 
            return newPerson;
 
       }
 
       public override string ToString()
 
       {
 
            return (Name + " : Dude!, I have a " + RightShoe.Color  +
 
                " shoe on my right foot, and a " +
 
                 LeftShoe.Color + " on my left foot." );
 
       }
  }
?
1
  
?
1
我们的Dude是一个引用类型,但是Shoe是一个结构体,属于值类型,在堆栈上的反应是这样的:
?
1
  
?
1
当我们执行以下方法的时候:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public static void Main()
 
{
 
     Class1 pgm = new Class1();
 
 
 
        Dude Bill = new Dude();
 
        Bill.Name = "Bill" ;
 
        Bill.LeftShoe = new Shoe();
 
        Bill.RightShoe = new Shoe();
 
        Bill.LeftShoe.Color = Bill.RightShoe.Color = "Blue" ;
 
 
 
        Dude Ted =  Bill.CopyDude();
 
        Ted.Name = "Ted" ;
 
        Ted.LeftShoe.Color = Ted.RightShoe.Color = "Red" ;
 
 
 
        Console.WriteLine(Bill.ToString());
 
        Console.WriteLine(Ted.ToString());           
 
}

 

我们得到了预期的结果:

 

Bill : Dude!, I have a Blue shoe on my right foot, and a Blue on my left foot. Ted : Dude!, I have a Red shoe on my right foot, and a Red on my left foot.

 

如果我们把Shoe改成引用类型,会是怎样一种情况呢?问题就在于此。我们将Shoe改成引用类型(class):

 

?
1
2
3
4
public class Shoe{
 
                public string Color;
            }

 

让后再次执行Main()方法,我们得到的结果是:

 

Bill : Dude!, I have a Red shoe on my right foot, and a Red on my left foot Ted : Dude!, I have a Red shoe on my right foot, and a Red on my left foot

 

注意到红色部分,这显然是一个错误。这是我们在堆里面得到的结果:

 

由于我们现在使用Shoe作为引用类型而不是值类型,而,当我们拷贝引用类型的内容时,我们只是拷贝了指针。我们必须做一些额外的工作,让我们的引用类型看起来更像是值类型。

 

幸运的是,我们有一个接口可以帮助我们解决这个问题:ICloneable。这个接口是所有的Dudes都遵守的契约,并且定义了一个应用类型是如何复制的,以防止我们的”共享鞋(shoe sharing)”的错误。所有的类要能被clone,都需要实现ICloneable接口,我们的Shoe也不例外。

 

ICloneable包含一个方法:Clone():

 

?
1
2
3
4
5
public object Clone()
 
                   {
 
                   }

 

我们将Shoe类改成如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Shoe : ICloneable
 
{
 
      public string Color;
 
      #region ICloneable Members
      public object Clone()
 
      {
 
          Shoe newShoe = new Shoe();
 
          newShoe.Color = Color.Clone() as string ;
 
          return newShoe;
 
      }
      #endregion
}

 

 

在Clone方法中,我们只是new了一个Shoe,克隆了所有的引用类型和拷贝了所有的值类型,然后返回一个新的object。也许你已经注意到了,string已经实现了ICloneable,因此我们可以直接调用Color.Clone()。由于Clone()方法是返回引用类型的,因此,在我们设定shoe的color之前我们必须重新设定引用的类型(retype the reference).

 

下一步,在我们的CopyDude()方法中,我们需要克隆shoes而不是拷贝他们:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public Dude CopyDude()
 
{
 
     Dude newPerson = new Dude();
 
      newPerson.Name = Name;
 
      newPerson.LeftShoe = LeftShoe.Clone() as Shoe;
 
      newPerson.RightShoe = RightShoe.Clone() as Shoe;
 
      return newPerson;
}

 

 

现在,当我们运行Main()方法:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public static void Main()
 
{
 
     Class1 pgm = new Class1();
 
        Dude Bill = new Dude();
 
        Bill.Name = "Bill" ;
 
        Bill.LeftShoe = new Shoe();
 
        Bill.RightShoe = new Shoe();
 
        Bill.LeftShoe.Color = Bill.RightShoe.Color = "Blue" ;
 
        Dude Ted =  Bill.CopyDude();
 
        Ted.Name = "Ted" ;
 
        Ted.LeftShoe.Color = Ted.RightShoe.Color = "Red" ;
 
 
        Console.WriteLine(Bill.ToString());
 
        Console.WriteLine(Ted.ToString());           
}

 

我们得到的结果:

Bill : Dude!, I have a Blue shoe on my right foot, and a Blue on my left foot Ted : Dude!, I have a Red shoe on my right foot, and a Red on my left foot

 

这正是我们需要的:

 

将东西包装起来(Wrapping Things Up)

 

作为通常的情况,我们总是想要克隆引用类型和拷贝值类型。(这将减少你为了防止头疼而不得不买的阿司匹林,当你调试这些错误的时候)

 

为了减少令人头痛的事,让我们更进一步,来整理Dude类,使它是想ICloneable,而不是使用CopyDude()方法。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class Dude: ICloneable
 
{
 
      public string Name;
 
      public Shoe RightShoe;
 
      public Shoe LeftShoe;
 
 
      public override string ToString()
 
      {
 
           return (Name + " : Dude!, I have a " + RightShoe.Color  +
 
               " shoe on my right foot, and a " +
 
                LeftShoe.Color + " on my left foot." );
 
          }
 
        #region ICloneable Members
 
 
 
        public object Clone()
 
        {
 
             Dude newPerson = new Dude();
 
             newPerson.Name = Name.Clone() as string ;
 
             newPerson.LeftShoe = LeftShoe.Clone() as Shoe;
 
             newPerson.RightShoe = RightShoe.Clone() as Shoe;
 
             return newPerson;
 
        }
 
        #endregion
   }

 

我们也需要修改Main()方法来使用Dude.Clone():

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public static void Main()
 
{
 
     Class1 pgm = new Class1();
 
 
 
        Dude Bill = new Dude();
 
        Bill.Name = "Bill" ;
 
        Bill.LeftShoe = new Shoe();
 
        Bill.RightShoe = new Shoe();
 
        Bill.LeftShoe.Color = Bill.RightShoe.Color = "Blue" ;
 
 
 
        Dude Ted =  Bill.Clone() as Dude;
 
        Ted.Name = "Ted" ;
 
        Ted.LeftShoe.Color = Ted.RightShoe.Color = "Red" ;
 
 
 
        Console.WriteLine(Bill.ToString());
 
        Console.WriteLine(Ted.ToString());           
}

 

我们最后的输出是:

Bill : Dude!, I have a Blue shoe on my right foot, and a Blue on my left foot. Ted : Dude!, I have a Red shoe on my right foot, and a Red on my left foot.

 

一切都正常。

 

一些值得注意的有趣的事是System.String的赋值操作(=操作符)事实上克隆了string,所以你不必担心重复的引用。然而,你需要注意防止内存膨胀。如果你看了上面的图标,由于string是一个引用类型,它需要指向堆里的另外一个对象,但为了简单,它显示成一个值类型。

 

总结:

通常的,如果我们有打算拷贝我们的对象,我们需要实现ICloneable。这样可以使我们的引用类型在行为上看起来有点像值类型。正如你所看到的,记录我们正在处理的是那种类型变量是很重要的,因为值类型和引用类型在分配内存时是不一样的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值