对象类的受保护方法 MemberWiseClone()

发布于 2024-09-03 01:23:29 字数 158 浏览 4 评论 0原文

这可能是一个愚蠢的问题,但我不明白:

我有一个名为 Card 的类。我想使用 MemberWiseClone() 进行浅克隆。 理论上Card继承自Object。所以它应该能够使用 MemberWiseClone(),即使 MWC() 受到保护?

我错过/忘记了什么吗?

This might be a dumb question, but I don't get it:

I have a class called Card. I want to do a shallow clone using MemberWiseClone().
In Theory Card inherits from Object. So it should be able to use MemberWiseClone(), even if MWC() is protected ??

Am I missing/forgetting something?

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

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

发布评论

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

评论(2

泛滥成性 2024-09-10 01:23:29

刷卡就可以用。

class Card
{
   public Card Clone()
   {
      return (Card)MemberwiseClone();
   }
}

Card can use it.

class Card
{
   public Card Clone()
   {
      return (Card)MemberwiseClone();
   }
}
醉南桥 2024-09-10 01:23:29

您只能对代码所在类的(编译时)类型的对象调用受保护的方法。

因此,任何特定类只能在该类的实例上调用 MemberwiseClone
由于您的类不是 Card(并且不继承 Card),因此您无法在 Card< 上调用 MemberwiseClone /代码> 实例。

You can only call a protected method on an object of the (compile-time) type of the class that your code is in.

Therefore, any particular class can only call MemberwiseClone on an instance of that class.
Since your class isn't Card (and doesn't inherit Card), you can't call MemberwiseClone on a Card instance.

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