简体   繁体   English

具有子类和继承的C#类:在继承的类中设置值

[英]C# Class With Sub Class And Inheritance: setting value in inherited class

I am fairly new to C# so be gentle. 我对C#相当陌生,所以要保持谦虚。
My end goal is to have a few different types of classes for inheritance (example below: Level ). 我的最终目标是为继承提供几种不同类型的类(下面的示例: Level )。 These inherited classes do two things, have .Value property with different setters depending on the class, and a .Action method, unique for every instance so it is an override. 这些继承的类有两个作用,一个是.Value属性,该类的值取决于类,另一个.Action方法对于每个实例都是唯一的,因此它是一个覆盖。

Question: how do i set the .Value property from a method on the class. 问题:如何通过类上的方法设置.Value属性。 This is the line of code (line 27) that does not work from the example below. 这行代码(第27行)在以下示例中无效。 And I don't understand why. 而且我不明白为什么。

 Potentiometer.Value = Convert.ToDouble(data);

// Code taken from https://dotnetfiddle.net/YNJpjf
using System;

namespace Main {
    class Program
    {
        static Device device = new Device();

        static void Main(string[] args)
        {
            device.HandleDataUpdate("50");
        }

    }

    public class Device
    {
        public class Potentiometer : Level
        {
            public override void Action()
            {
                Console.WriteLine("value is at: {0}", this.Value);
            }
        }

        public void HandleDataUpdate(String data)
        {
            Potentiometer.Value = Convert.ToDouble(data);
        }
    }

    public class Level
    {
        private Double thisValue;
        public Double Value
        {
            get => thisValue;
            set
            {
                if (thisValue != value)
                {
                    thisValue = value;
                    Action();
                }
            }
        }

        public virtual void Action()
        {

        }
    }
}

Give Level a protected abstract method OnSetValue , forcing each subclass to override it. Level一个受保护的抽象方法OnSetValue ,强制每个子类重写它。 Now the setter of Value can call OnSetValue before the value is set: 现在, Value的设置者可以在设置Value之前调用OnSetValue

public class Level {
    private double val;
    public double Value {
        get {
            return val;
        }
        set {
            OnSetValue(value);
            val = value;
        }
    }
    protected abstract void OnSetValue(double val);
    ...
}
public class Potentiometer : Level {
    protected void OnSetValue(double val) {
        ...
    }
}

You need to create an instance of Potentiometer. 您需要创建一个电位计实例。

https://dotnetfiddle.net/8JQDyO https://dotnetfiddle.net/8JQDyO

You're trying to set value of class field, which is impossible, not of object's field. 您正在尝试设置类字段的值,这是不可能的,而不是对象字段的值。 You have to make instance of Potentiometer inside the Device , if you want to do this , you could make method or change code logic. 您必须在Device内部创建Potentiometer实例,如果要执行此操作,则可以创建方法或更改代码逻辑。

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

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