ICloneable问题

发布于 2024-10-05 00:43:47 字数 46 浏览 4 评论 0原文

如果一个类实现了ICloneable,那意味着什么?

If a class implemented ICloneable, what does that mean?

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

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

发布评论

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

评论(4

谜兔 2024-10-12 00:43:47

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.

枕花眠 2024-10-12 00:43:47

它只是意味着该类必须实现一个返回对象的方法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.

小糖芽 2024-10-12 00:43:47

基本上,它只允许克隆类:

http://msdn。 microsoft.com/en-us/library/system.icloneable.aspx

实现任何接口时,都需要在该接口中定义方法。在这种情况下,需要在您的类中定义 Clone 方法。

微软的例子:

public object Clone()
{
    return this.MemberwiseClone();
}

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:

public object Clone()
{
    return this.MemberwiseClone();
}
素罗衫 2024-10-12 00:43:47

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.

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