对象库 - 访问索引值还是对象本身? (例如,深拷贝与浅拷贝也许?)

发布于 2024-09-08 01:20:58 字数 796 浏览 11 评论 0原文

我一直对 .Net 如何复制引用感到困惑/不确定。 假设我有一个 GDI+ 位图对象。

dim foo as new bitmap("c:\foo.bmp")

'Foo' 保存位图对象。 现在假设我这样做。

dim bar as bitmap = foo

这是浅拷贝还是深拷贝?如果我将 foo 设置为无,那么 bar 是否也会突然引用“无”?或者 bar 是否也包含位图的副本,为了从内存中完全删除位图,我需要将 'foo' 和 'bar' 设置为空?

我需要在内存中保留一个位图库,对我来说,将每个创建的对象中每个位图的引用存储为变量会更容易,而不是使用索引对其进行编码并每次都必须引用该库它是需要的(例如“BitmapLibrary.Singleton.getBitmap(id)”)

简而言之,我可以这样做:

struct graphic object
    dim myBitmap as bitmap

    sub draw(g as graphics)
          g.drawimage(myBitmap)
    end sub

而不是这样:

struct graphic object
    dim myBitmapIndex as integer

    sub draw(g as graphics)
          g.drawimage(bitmaplibrary.getImage(myBitmapIndex))
    end sub

I've always been confused/unsure about how .Net copies references.
Let's say I have a Bitmap object for GDI+.

dim foo as new bitmap("c:\foo.bmp")

'Foo' holds the bitmap object.
Now let's say I do this.

dim bar as bitmap = foo

Is this a shallow copy or a deep copy? If I set foo equal to nothing, does bar suddenly reference 'nothing' as well? Or does bar contain a copy of the bitmap as well, and in order to remove the bitmap from memory completely, I need to set both 'foo' and 'bar' to nothing?

I need to keep a library of bitmaps in memory, and to me it would be easier to just store a reference to each bitmap in each created object as a variable, instead of coding it with an index and having to refer to the library each time it is needed (such as 'BitmapLibrary.Singleton.getBitmap(id)')

In a nutshell, can I do this:

struct graphic object
    dim myBitmap as bitmap

    sub draw(g as graphics)
          g.drawimage(myBitmap)
    end sub

instead of this:

struct graphic object
    dim myBitmapIndex as integer

    sub draw(g as graphics)
          g.drawimage(bitmaplibrary.getImage(myBitmapIndex))
    end sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖公主 2024-09-15 01:20:59

一般来说,对象的引用只是复制引用。对于从 System.ValueType 继承的类型来说,情况并非如此,它是在 System.ValueType 中复制的。

例如:

Dim foo as new Bitmap("C:\foo.bmp")
Dim bar as Bitmap = foo

''//Now close foo
foo.Dispose()
''//Access to bar fails, since the object it was pointing to was the same as foo
Console.WriteLine(bar.PixelHeight)

对于 System.ValueType 后代:

Dim p1 as new Point(1, 2)
Dim p2 as Point = p1

p1.Offset(1, 0)

Console.WriteLine(p1)  ''//Prints (2, 2)
Console.WriteLine(p2)  ''//Prints (1, 2)

如果我将 foo 设置为 Nothing,那么我只是将引用指向其他内容 (Nothing),并且没有更改 bar 的对象参考文献。

如果要存储对象列表,将它们存储在列表结构中似乎是最谨慎的做法。但是,如果您知道您只会拥有固定的少量对象,那么将引用保留为类的字段可能是一个可行的选择。在您的情况下,您通过方法 getImage 抽象访问,这是一个很好的设计选择,因为这样您的类的用户就不必费心了解您跟踪引用的策略,并且您应该能够根据需要更改它,而不会破坏依赖于您的类的对象。

In general, references to objects just copy the reference. This is not true for types which inherit from System.ValueType, where it is copied.

For example:

Dim foo as new Bitmap("C:\foo.bmp")
Dim bar as Bitmap = foo

''//Now close foo
foo.Dispose()
''//Access to bar fails, since the object it was pointing to was the same as foo
Console.WriteLine(bar.PixelHeight)

For System.ValueType descendants:

Dim p1 as new Point(1, 2)
Dim p2 as Point = p1

p1.Offset(1, 0)

Console.WriteLine(p1)  ''//Prints (2, 2)
Console.WriteLine(p2)  ''//Prints (1, 2)

If I set foo to Nothing, then I've just made the reference point to something else (Nothing) and haven't changed the object which bar references.

If you are storing a list of objects, storing them in a list structure seems most prudent. However, if you know that you are only going to ever have a fixed, small number of objects, then perhaps keeping the references as fields of a class is a viable option. In your case, you abstract access through the method getImage which is a good design choice, since then users of your class won't have to bother knowing your strategy for keeping track of references, and you should be able to change it as needed without breaking objects which depend on your class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文