我对C#相当新,所以要温和。
我的最终目标是有几个不同类型的继承(例如:级别)。这些继承的类做了两件事,具有.Value属性,具有取决于类的不同setter和.Action方法,对每个实例都是唯一的,因此它是重写。
问题:如何设置。值属性来自类上的方法。这是代码行(第27行),下面的示例不起作用。我不明白为什么。C#类与子类和继承:在继承类中的设置值
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()
{
}
}
}
为什么在'Device'里声明'Potentiometer'类? – ja72