Object Class's protected method MemberWiseClone()

て烟熏妆下的殇ゞ 提交于 2019-12-24 21:38:59

问题


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?


回答1:


Card can use it.

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



回答2:


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.



来源:https://stackoverflow.com/questions/2914378/object-classs-protected-method-memberwiseclone

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!