我有一个P类作为命名空间D的一部分,有几个字段和相关的属性
namespace Driver
[Export(typeof (P))]
public class Pilot : Send
{
private bool _b1;
...
public bool B1
{
get { return _b1; }
private set
{
if (_b1 != value)
{
_b1 = value;
NotifyOfPropertyChange(() => B1);
}
}
}
然后使用某些方法在同一命名空间中的另一个类
namespace Driver
public class PilotEng
{
public void Statistics()
{
....
}
public void Running()
{
....
}
在PE类方法中访问和使用P类参数的最佳方法是什么?
答案 0 :(得分:0)
PilotEng
有多种方法可以从Pilot
访问信息。
在Pilot
构建时传递PilotEng
的实例:
public class PilotEng
{
private Pilot myPilot;
public PilotEng(Pilot pilot)
{
myPilot = pilot;
}
public void Statistics()
{
var whatever = myPilot.B1;
....
}
public void Running()
{
....
}
}
其他地方......
public void SomeMethod()
{
Pilot p = new Pilot();
PilotEng pe = new PilotEng(p);
pe.Statistics();
}
更新您的方法签名,以便将导航实例用于:
public class PilotEng
{
public void Statistics(Pilot pilot)
{
var whatever = pilot.B1;
....
}
public void Running()
{
....
}
}
其他地方......
public void SomeMethod()
{
Pilot p = new Pilot();
PilotEng pe = new PilotEng();
pe.Statistics(p);
}
两者都有效,一个可能比另一个更有效,还有其他几种方法可以实现这一点。这一切都取决于你真正想要做的事情。