开发者

Object Class's protected method MemberWiseClone()

开发者 https://www.devze.com 2022-12-31 22:35 出处:网络
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().

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 ??

开发者_Go百科

Am I missing/forgetting something?


Card can use it.

class Card
{
   public Card Clone()
   {
      return (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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号