سؤال

I'm extending a libary class which has a public Method that I don't wan't to be callable from the outside. I know that I can hide it using the "new" keyword, but the problem is, that it has a lot of different declarations. Something like this:

class Parent() {
  public double method(double a, double b) { ... }
  public int method(int a, int b) { ... }
  public System.Int32 method(System.Int32 a, System.Int32 b) { ... }
}

Now I could do:

class Child() {  
  // Make method private.
  private new double method(double a, double b) { return base.method(a, b) }
  private new int method(int a, int b) { return base.method(a, b) }
  private new System.Int32 method(System.Int32 a, System.Int32 b) { return base.method(a, b) }

  /// Own Code.
}

But I'm wondering if there is not a simpler way to make all overrides of that function private.

هل كانت مفيدة؟

المحلول 2

That's not the way method hiding works. Your private new methods only hide the original methods inside the Child class (but they call what they hide, so nothing's gained).

In a Grandchild class deriving from Child, nothing is hidden anymore because the private new methods are not in scope, so the public methods of Parent are perfectly callable and "naked". Method hiding does not remove the hidden method in any way; it's just a matter of which of the identical methods gets called.

نصائح أخرى

I think the first question is whether or not your descendant class needs to be cast as the parent.

If not, then the best way would be to use a facade pattern in which you hide the "parent" and only allow access to it through your methods.

Kind of like:

class Parent {
  public double method(double a, double b) { ... }
  public int method(int a, int b) { ... }
}


class YourStuff {
  private Parent _parent = new Parent();

  public int AddNumbers(int a, int b) {
     return _parent.method(a,b);
  }
}

In this case, the Parent class is hidden.

If you want that your library class Parent method which name Method will not be callable from the outside you need to change access modifier. Your class hasn't access modifier, so, be default it is Internal - The type or member can be accessed by any code in the same assembly, but not from another assembly. The same accessibility is for method Method as it has access modifier public. If you want that implemented in class Method can be accessible only for derived class instance you need all access modifiers public for overloaded Method variations to change to protected and then in a child class hide this method overloaded methods. If you want these hidden methods to be used from outside you need to change their access modifiers from private to public. Example of this implementation:

namespace HideBaseClassMethod
{
    class Program
    {
        static void Main(string[] args)
        {
            Parent myParent = new Parent();
            //myParent. - no one of overloaded protected methods are accessible after dot operator
           Child myChild = new Child();
           myChild.Method(2.5, 4.3); //double 
           myChild.Method(2, 4);//int

           Parent myChildCreatedAsParent = new Child();
           //myChildCreatedAsParent.  - no one of overloaded protected methods are accessible after dot operator
        }
    }
    class Parent 
    {
        protected double Method(double a, double b)
        {
           Console.WriteLine($"I am a parent double method {a} + {b}");
           return a + b;
        }
        protected int Method(int a, int b)
       {
            Console.WriteLine($"I am a parent integer method {a} + {b}");
            return a + b;
       }
   }
   class Child:Parent
   {
        public new double Method(double a, double b)
        {
            Console.WriteLine($"I am a child double method {a} * {b}");
            return a * b;
        }
        public new int Method(int a, int b)
        {
            Console.WriteLine($"I am a child integer method {a} * {b}");
            return a + b;
        }
    }
}

The result of this program execution will be:

I am a child double method 2.5 * 4.3

I am a child integer method 2 * 4

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top