Is there way for a class to 'remove' methods that it has inherited?

后端 未结 4 1730
梦如初夏
梦如初夏 2020-12-29 22:28

Is there way for a class to \'remove\' methods that it has inherited?

E.g. if I don\'t want my class to have a ToString() method can I do something so t

4条回答
  •  星月不相逢
    2020-12-29 23:12

    As others pointed out, you can't "remove" a method, but if you feel it has wronged you in some way you can hide it in your derived class.

    From Microsoft's documentation (now retired):

    class Base
    {
       public static void F() {}
    }
    class Derived: Base
    {
       new private static void F() {}   // Hides Base.F in Derived only
    }
    class MoreDerived: Derived
    {
       static void G() { F(); }         // Invokes Base.F
    }
    

提交回复
热议问题