ICloneable问题
如果一个类实现了ICloneable
,那意味着什么?
If a class implemented ICloneable
, what does that mean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如果一个类实现了ICloneable
,那意味着什么?
If a class implemented ICloneable
, what does that mean?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
即具有
IClonable.Clone
< /a> 方法。文档说该方法旨在克隆对象。该文档特别指出,克隆可以是深克隆,也可以是浅克隆。还需要注意的是,结果类型必须与克隆的对象具有相同的类型,但类型系统不能保证它实际上是这样。总而言之,它没有提供太多硬性承诺,但其目的是创建独立的克隆。
That is has the
IClonable.Clone
method. The documentation says that the method is intended to clone objects. The documentation notes specifically that the clone can be either deep or shallow. It is also noted the the resulting type must be of the same type as the object that is cloned, but there is no guarantee in the type system that it actually is so.To sum it up, it does not offer much hard promises, but the intent is to create independent clones.
它只是意味着该类必须实现一个返回对象的方法
Clone
,仅此而已。因此,您可以有一个接受 ICloneable 的方法,然后您可以克隆该对象。It just means that the class must implement a method
Clone
that returns an object, not more than that. So you can have a method that accepts an ICloneable and then you can clone that object.基本上,它只允许克隆类:
http://msdn。 microsoft.com/en-us/library/system.icloneable.aspx
实现任何接口时,都需要在该接口中定义方法。在这种情况下,需要在您的类中定义 Clone 方法。
微软的例子:
Basically, it just allows the class to be cloned:
http://msdn.microsoft.com/en-us/library/system.icloneable.aspx
When implementing any interface, you are required to define the methods in that interface. In this case, the Clone method will need to be defined in your class.
Example from Microsoft:
ICloneable 本身没有意义,但与其他约束结合起来可能很有用(例如,可以规定参数必须是实现 ICloneable 的 Foo)。因此,可以有一个 Foo、一个 CloneableFoo、一个 AdvancedFoo 和一个 CloneableAdvancedFoo,从而允许将支持克隆的 Foo 派生与那些不支持克隆的派生区分开来,而且还允许期望可克隆 Foo 的例程接受 Foo 的可克隆派生。
不幸的是,虽然使用 IClonable 和 Foo 约束传递的函数参数可以用作 IClonable 和 Foo,但如果没有类型转换,则无法创建满足此类条件的字段,也没有任何方法对字段进行类型转换。对此的补救措施可能是创建一个 ICloneable(Of T As ICloneable(Of T)),其中包含一个返回 T 的“Clone”方法和一个也返回 T 的“Self”方法(因此,一个包含“ ICloneable Of Foo”可以通过“Self”方法作为 Foo 进行访问)。需要稍微小心才能使这一切正常工作,但模式应该相当不错。
ICloneable is not meaningful by itself, but may be useful in conjunction with other constraints (e.g. one could provide that a parameter must be a Foo that implements ICloneable). Thus, one could have a Foo, a CloneableFoo, an AdvancedFoo, and a CloneableAdvancedFoo, allowing Foo derivatives that support cloning to be distinguished from those that do not, but also allowing routines that expect a cloneable Foo to accept a cloneable derivative of Foo.
Unfortunately, while a function parameter passed with IClonable and Foo constraints may be used as an IClonable and as a Foo, without typecasts, there's no way to create a field meeting such criteria, nor is there any way to typecast a field. A remedy for this may be to create an ICloneable(Of T As ICloneable(Of T)), which contains a "Clone" method that returns T, and a "Self" method which also returns T (Thus, a field holding an "ICloneable Of Foo" could be accessed as a Foo via the "Self" method). A little care would be needed to make this all work, but the pattern should be quite nice.