简体   繁体   English

C#中的继承变量

[英]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() . 假设您有一个名为IMath的接口,其中包含一个方法string DoMath() Is it possible to have a variable written in the IMath interface that all classes implementing that interface would see new values? 是否可以在IMath接口中编写一个变量,以使实现该接口的所有类都可以看到新值? 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. 因此,例如,我的课Add : IMath将有法DoMath()并在DoMath()方法会改变变量的值double ITotal它实现了IMath接口的所有类将看到新的价值。

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. 有关更多信息,请参见接口上的C#文档

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". 属性可以理解为“检索X值的能力”或“提供X值的能力”,其中变量为“存储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 : 如果希望从MathBase继承的每个类的每个实例共享相同的Total值,则可以将其声明为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";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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