How to hide static method

后端 未结 3 706
悲哀的现实
悲哀的现实 2021-01-15 09:00

Let\'s say I have a classes, like that:

class A
{
   public static int Count()
}
class B : A
{
}
class C : A
{
}

How can I hide this static

相关标签:
3条回答
  • 2021-01-15 09:30

    The only solution would be to change your class hierarchy. It's not worth the hassle and WTF moments you will get in code reviews it if you ask me.

    class ABase
    {
    }
    class A
    {
       public static int Count()
    }
    class B : ABase
    {
    }
    class C : ABase
    {
    }
    
    0 讨论(0)
  • 2021-01-15 09:34

    You could do it by creating another class, let's call it Special, that inherits A. Then you would make C inherit from Special and B inherit from A. Also, you would have the static method protected, that means only classes that inherited Special will have access to it.

    class A
    {
    }
    class Special : A
    {
        protected static int Count()
    }
    class B : A
    {
    }
    class C : Special
    {
    }
    
    0 讨论(0)
  • 2021-01-15 09:39

    You can't, basically. Heck, if it's public then anyone can call it.

    You could make it protected which would allow it to be called from within B or C but not elsewhere... but you still couldn't differentiate between B and C.

    0 讨论(0)
提交回复
热议问题