下面是我正在做的一個簡單示例。基本上,我有多個接口,可以通過1個類或單獨的類實現,因此我將每個接口存儲在應用程序中。我唯一的問題是關於變量myInterface,yourInterface和ourInterface。 他們引用同一個對象還是有3個不同的對象?在.Net中實現多個接口和對象實例
interface IMyInterface
{
void MyFunction();
}
interface IYourInterface()
{
void YourFunction();
}
interface IOurInterface()
{
void OurFunction();
}
public class MainImplementation : IMyInterface, IYourInterface, IOurInterface
{
public void MyFunction() { }
public void YourFunction() { }
public void OurFunction() { }
}
private IMyInterface myInterface;
private IYourInterface yourInterface;
private IOurInterface ourInterface;
static void Main(string[] args)
{
myInterface = new MainImplementation() as IMyInterface;
yourInterface = myInterface as IYourInterface;
ourInterface = myInterface as IOurInterface;
}
獎勵:有沒有更好的方法來做到這一點?
我對這個問題的標題有點困惑。 ByVal或ByRef與提供的代碼示例有什麼關係?沒有任何方法採用任何參數。 – JohnFx 2009-08-19 01:15:43
修正了它。希望,這有點更好。這是一個漫長的一天:) – kevindaub 2009-08-19 01:17:28