我有一个名为PINPAD
的类,一个名为Devices
的基类以及两个名为DevA
和Dev B
的派生类
问题在于,主类属性PINPAD.Status
需要从类DevA
和DevB
进行更新
代码如下:
public class CALLER()
{
CALLER()
{
PINPAD PINPD = new PINPAD("A");
//....
MessageBox.Show(PINPD.Status);
}
}
public class PINPAD()
{
public Devices MyDev = null;
public string Status = "";
public PINPAD(string Model)
{
if (Model = "A")
{
DevA MyDev = new DevA()
}
else
{
DevB MyDev = new DevB()
}
MyDev.DoWork();
}
}
public class Devices
{
public virtual void DoWork() {}
}
public class DevA : Devices
{
public override void DoWork() { }
DevA()
{
PINPAD.Status = "Broken";
//Status not avialable!
}
}
public class DevB : Devices
{
public override void DoWork() { }
DevB()
{
PINPAD.Status = "Working!";
//Status not avialable!
}
}
答案 0 :(得分:0)
您已将PINPAD.Status
声明为实例字段,这意味着您必须具有PINPAD
类型的对象才能访问它。
var myPinpad = new PINPAD();
myPinpad.Status = "Whatever";
如果只希望一个字段随处可见,请声明字段static
:
public static string Status = "";