Hide a base class method in a generic derived class

后端 未结 2 1711
别跟我提以往
别跟我提以往 2021-01-19 06:21

I have a base class like this:

class FooBase
{
    public bool Do(int p) { /* Return stuff. */ }
}

And a child class like this:

<         


        
2条回答
  •  暖寄归人
    2021-01-19 06:47

    You could build a base class that is abstract with a protected Do method, and rewrite your current FooBase class to inherit from Foo:

    public abstract class FooBaseAbstract
    {
        protected bool Do(int p)
        {
            return true;
        }
    }
    
    // You can use this one just as your current FooBase class
    public class FooBase : Foo
    {
    }
    
    public class Foo : FooBaseAbstract
    {
        public bool Do(T p)
        {
            if (true /* some test here */)
            {
                return base.Do(4);
            }
    
            return false;
        }
    }
    

    (of course change the class names)

提交回复
热议问题