简体   繁体   中英

Inheritance variables in c#

Just for practice I'm trying to write a calculator program. To make it difficult I trying to use some of the advanced inheritance topics I've learned, but not really used. Lets say you have an interface named IMath with one method string DoMath() . Is it possible to have a variable written in the IMath interface that all classes implementing that interface would see new values? So for example my class Add : IMath would have the method DoMath() and in that DoMath() method would change the value of the variable double ITotal which all classes that implement the IMath interface would see the new value.

You cannot specify variables or fields in interfaces, you can only specify:

  • Methods
  • Properties
  • Indexers
  • Events

See the C# documentation on interfaces for more information about this.

An interface dictates expected behavior, it does not dictate expected implementation. A property can be read as "the ability to retrieve the value of X" or "the ability to provide the value of X", where as a variable is "the ability to store X". This is not the same thing, and interfaces cannot make that guarantee.

If you absolutely need to enforce the presence of a variable, you should use a base class. I would probably look into combining these things, use interfaces for the external interface (ie. how should my calculator function) and base class and inheritance to avoid rewriting the same code over and over.

It sounds like what you're looking for is an abstract base class.

One possible implementation of what you describe is shown below.

public abstract class MathBase
{
    public double Total { get; protected set; }

    public abstract string DoMath(double value);

    protected double ParseValue(string value)
    {
        double parsedValue;

        if (!double.TryParse(value, out parsedValue))
        {
            throw new ArgumentException(string.Format("The value '{0}' is not a number.", value), "value");
        }

        return parsedValue;
    }
}

public class Add : MathBase
{
    public override string DoMath(string value)
    {
        Total += ParseValue(value);

        return Convert.ToString(Total);
    }
}

If you wanted every instance of every class that inherits from MathBase to share the same Total value, you would declare it as static :

public abstract class MathBase
{
    public static double Total { get; protected set; }

    public abstract string DoMath(string value);
}

(although I'm not really sure why you would want this)

You could do something like this:

interface IMath
{
    string DoMath();
}

abstract class MathBase : IMath
{
    protected double Total { get; set; }

    public abstract string DoMath();
}

class Add : MathBase
{
    public override string DoMath()
    {
        this.Total = 2;

        return "2";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM