读书人

interface解决命名冲突后的函数如何用

发布时间: 2012-12-15 15:16:03 作者: rapoo

interface解决命名冲突后的函数怎么用?

public interface IDoSomething
{
void Test();
}
public interface IOther
{
string Test();
}

/// <summary>
///Class1 的摘要说明
/// </summary>
public class Class1 : IDoSomething, IOther
{
public Class1()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public void Test()
{
throw new NotImplementedException("IDoSomething");
}

string IOther.Test()
{
return "IOther";
}

}


string IOther.Test()
{
return "IOther";
}
这里解决了命名冲突,但是客户端怎么用这个函数啊?


public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Class1 c = new Class1();
Response.Write(IOther.Test());
}
}

[最优解释]
(c As IOther).Test();
[其他解释]
http://www.cnblogs.com/Ivony/archive/2010/05/17/1737037.html
[其他解释]
引用:
引用:(Class1 As IOther).Test();
这个又让我想到另一个问题,就是,要是这个函数是static的怎么办?也就是不需要生成对象就可以调用。

想多了,static函数不能继承,不能实现接口。
[其他解释]
引用:
引用:(Class1 As IOther).Test();
这个又让我想到另一个问题,就是,要是这个函数是static的怎么办?也就是不需要生成对象就可以调用。
http://stackoverflow.com/questions/259026/why-doesnt-c-sharp-allow-static-methods-to-implement-an-interface
[其他解释]
(Class1 As IOther).Test();
[其他解释]
引用:
(Class1 As IOther).Test();

这个又让我想到另一个问题,就是,要是这个函数是static的怎么办?也就是不需要生成对象就可以调用。

读书人网 >C#

热点推荐