overriding a function c#

混江龙づ霸主 提交于 2019-12-07 02:05:49

问题


I was just trying to master the concept of virtual function using a console app. I noticed as soon as I override a base class function, return baseclassname.functionname(parameters) gets inserted in my function body automatically. Why does this happen?

class advanc
{
    public virtual int  calc (int a , int b)
    {        
         return (a * b);        
    }        
}

class advn : advanc
{
     public override int calc(int a, int b)
     {
          //automatically inserted
          return base.calc(a, b);
      }        
}

回答1:


By overriding a virtual function you expand the functionality of your base class and then call the base class for the 'base functionality'.

If you would remove the 'return base.calc(a,b)' line, the base class code would not execute.

If you want to completely replace the functionality this is not a problem but if you want to extend the functionality then you should also call the base class.

The following code demonstrates this (just put it in a Console Application)

class advanc
{
    public virtual int calc(int a, int b)
    {
        Console.WriteLine("Base function called");
        return (a * b);
    }
}

class advn : advanc
{
    public bool CallBase { get; set; }
    public override int calc(int a, int b)
    {
        Console.WriteLine("Override function called");

        if (CallBase)
        {
            return base.calc(a, b);
        }
        else
        {
            return a / b;
        }
    }
}

private static void Main()
{
    advn a = new advn();

    a.CallBase = true;
    int result = a.calc(10, 2);
    Console.WriteLine(result);

    a.CallBase = false;
    result = a.calc(10, 2);
    Console.WriteLine(result);
    Console.WriteLine("Ready");
    Console.ReadKey();
}



回答2:


Visual Studio is trying to guess what you want, so it suggests you this code.

Sometimes, the overridden functions expand the functionality of the base class functions. In this case it is viable to call base.function somewhere inside the overridden one.




回答3:


It means that when it calls base.calc it will call the parents function that it is overriding. If you don't want to call the base function you can remove it but in most cases it will remain (which is why visual studio auto generates it).

If in your advanced function you didn't want to call the a * b functionality you would remove the base function call and write your own.

E.G. if you declare this in your advn class :

public override int calc(int a, int b)
{
   return Math.Pow(a, b);
}

You will get the following when calling this functionality :

advanc myObj = new advanc();
advn advnObj = new advn();

myObj.calc(5, 2); // will return 10
advnObj.calc(5, 2); // will return 25



回答4:


the IDE is trying to generate the easiest possible overridden method that actually compiles (so if you leave the actual implementation for later, you still have a piece of code that does not do much but still compiles and yields a result that is probably not going to break the rest of your code).

Also, there are some scenarios where the call to the base class's method makes sense even after your implementation:

  1. the behaviour of your child class expands on the base but does not replace it completely
  2. you're using the decorator pattern but your calc method doesn't need to be changed (you still need to implement it because it's part of the original interface you're decorating, but you are just forwarding the call to your base class without changing the behavior)



回答5:


The IDE is simply inserting a call to the base implementation. This means that, by default, the behaviour of the override will be identical to the method it overrides. If this is not the desired behaviour (likely not), simply repace with the code of your choice.




回答6:


The base function body gets invoked automatically becouse of the line return base.calc(a, b); But, you can simply make the base function body doesn't gets invoked automatically, and invoked your owen like this:

    class advn : advanc
    {
        public override int calc(int a, int b)
        {
             return a * b; //anything else;
        }

    }



回答7:


By default VS offers to at least do what the parent does in the function. In addition, in many cases you need to run parent then do your action (e.g. in UI cases). for base see MSDN about it:

The base keyword is used to access members of the base class from within a derived class:

Call a method on the base class that has been overridden by another method.
Specify which base-class constructor should be called when creating instances 
of the derived class.

A base class access is permitted only in a constructor, an instance method, or an instance property accessor.



来源:https://stackoverflow.com/questions/8309419/overriding-a-function-c-sharp

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