C # Inheritance of confusion

I am trying to get method calls of the Base class and Derived class. However, I am a little confused if I do it right. I would like to set the values ​​from the Base class and use them in the Derived class.

namespace Inheritance
{
    using System;

    public class BaseClass
    {
        public BaseClass() { }

        protected string methodName;
        protected int noOfTimes;
        public void Execute(string MethodName, int NoOfTimes)
        {
            this.methodName = MethodName;
            this.noOfTimes = NoOfTimes;
        }
    }

    public class DerivedClass : BaseClass
    {
        public DerivedClass() : base() { }

        public void Execute()
        {
            Console.WriteLine("Running {0}, {1} times", base.methodName, base.noOfTimes);
        }
    }

    public class Program
    {
        static void Main(string[] args)
        {
            DerivedClass d = new DerivedClass();
            d.Execute("Func", 2);
            d.Execute();

            Console.ReadLine();
        }
    }
}

Question: Can I achieve the same as above using only 1 Execute call instead of 2?

Hope my above example is clear. Please let me know if this is not the case and I will provide further information.

thank

+3
source share
4 answers

Disclaimer , my answer suggests that you want the solution to revolve around the inheritance of the method and its implementation. Other answers are also a good solution to the question.

, . virtual abstract. . , , , , .

override, #. base.Execute(MethodName, NoOfTimes);, . .

base - , , , .

abstract , ( virtual, override).

namespace Inheritance
{
    using System;

    public class Program
    {
        internal protected class BaseClass
        {
            public BaseClass() { }

            protected string methodName;
            protected int noOfTimes;
            public virtual void Execute(string MethodName, int NoOfTimes)
            {
                this.methodName = MethodName;
                this.noOfTimes = NoOfTimes;
            }
        }

        internal class DerivedClass : BaseClass
        {
            public DerivedClass() : base() { }

            public override void Execute(string MethodName, int NoOfTimes)
            {
                base.Execute(MethodName, NoOfTimes);
                Console.WriteLine("Running {0}, {1} times", base.methodName, base.noOfTimes);
            }
        }

        static void Main(string[] args)
        {
            DerivedClass d = new DerivedClass();
            d.Execute("Func", 2);

            Console.ReadLine();
        }
    }
}

, .

+8

. Execute doWork, . , base.Execute, doWork.

abstract class BaseClass 
{ 
    public BaseClass() { } 

    protected string methodName; 
    protected int noOfTimes; 
    public void Execute(string MethodName, int NoOfTimes) 
    { 
        this.methodName = MethodName; 
        this.noOfTimes = NoOfTimes;
        doWork();
    } 

    protected abstract void doWork(); // will be provided by derived classes
} 

class DerivedClass : BaseClass 
{ 
    public DerivedClass() : base() { } 

    protected override void doWork()
    {
        Console.WriteLine("Running {0}, {1} times", this.methodName, this.noOfTimes); 
    } 
} 

public class Program   
{   
    static void Main(string[] args) 
    { 
        DerivedClass d = new DerivedClass(); 
        d.Execute("Func", 2); 

        Console.ReadLine(); 
    } 
}

. (), ( , , , doWork). , :

class BaseClass 
{ 
    public BaseClass() { } 

    protected string methodName; 
    protected int noOfTimes; 
    public void Execute(string MethodName, int NoOfTimes) 
    { 
        this.methodName = MethodName; 
        this.noOfTimes = NoOfTimes;
        doWork();
    } 

    protected virtual void doWork() {
        // default behaviour, can be modified by subclasses
    }
} 
+4

, .

public DerivedClass() : base() { }

public DerivedClass() { }

.

, , .:)

+1

, , , . , base.methodName .

( ) /, , . .

In addition, in your case, you define Execute with different signatures (i.e. a different number and / or type of parameters) in the base and derived classes. This means that you must name them separately, as they are different.

You can use virtual and override, as indicated by Adam, only if you define a version in a derived class with exactly the same parameters, even if they are not used by that version. BTW, here you are using the base, because you want to call the version of Execute defined in the base class.

+1
source

All Articles