问:如何在派生的类里面实现这两个接口的相同名称的方法呢?
对有相同名称的方法在实现时,前面不能加public等关键词。每个方法前必须冠以相应的接口名。
如果调用那个接口的实现,则必须将示例强制转换为相应接口类型。
比如:void IFace1.Print(){......};
void IFace2.Print(){......};
例:
public interface Myface2
{
int GetInt();
string GetString();
}
public interface Myface1
{
int GetInt();
}
public partial class WebForm2 : System.Web.UI.Page, Myface1, Myface2
{
int Myface1.GetInt()
{
return 10;
}
int Myface2.GetInt()
{
return 12;
}
public string GetString()
{
return "Stirng";
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Response.Write(this.GetString());
WebForm2 web2 = new WebForm2();
Response.Write(((Myface1)web2).GetInt());// 类的实例强制转化接口
}
}
}
浙公网安备 33010602011771号