如何在c#中调用接口特定方法?
我有一个单一的界面。在接口内部,我有3个方法如何在c#中调用接口特定方法?,c#,C#,我有一个单一的界面。在接口内部,我有3个方法 public interface interfaceMethod { string methodA(); string methodB(); string methodC(); } 我只想为methodA()调用一个方法,而不引用所有接口方法。如何只调用一个方法而不是引用所有方法 Public class Class1 { public string testCallInterfaceMethod() {
public interface interfaceMethod
{
string methodA();
string methodB();
string methodC();
}
我只想为methodA()调用一个方法,而不引用所有接口方法。如何只调用一个方法而不是引用所有方法
Public class Class1
{
public string testCallInterfaceMethod()
{
interfaceMethod obj = new interfaceMethod();
obj.callMethodA();
}
}
为什么要制定一个接口/合同,然后不遵守它?听起来像是两个接口的完美用例
public interface interfaceMethod
{
string methodA();
}
public interface anotherInterface : interfaceMethod
{
string methodB();
string methodC();
}
为什么要制定一个接口/合同,然后不遵守它?听起来像是两个接口的完美用例
public interface interfaceMethod
{
string methodA();
}
public interface anotherInterface : interfaceMethod
{
string methodB();
string methodC();
}
接口只是一个契约。类可以签署该契约,然后实现契约要求它执行的方法。使用您的界面,以下代码将起作用:
public interface InterfaceMethod
{
string methodA();
string methodB();
string methodC();
}
Public class Class1 : InterfaceMethod
{
//Implementation of InterfaceMethod interface
public string MethodA() { /* Code */ }
public string MethodB() { /* Code */ }
public string MethodC() { /* Code */ }
/* Class' other code */
}
在别的地方
public string testCallInterfaceMethod()
{
InterfaceMethod im = new Class1();
Console.WriteLine(im.MethodA());
Console.WriteLine(im.MethodB());
Console.WriteLine(im.MethodC());
return null;
}
接口只是一个契约。类可以签署该契约,然后实现契约要求它执行的方法。使用您的界面,以下代码将起作用:
public interface InterfaceMethod
{
string methodA();
string methodB();
string methodC();
}
Public class Class1 : InterfaceMethod
{
//Implementation of InterfaceMethod interface
public string MethodA() { /* Code */ }
public string MethodB() { /* Code */ }
public string MethodC() { /* Code */ }
/* Class' other code */
}
在别的地方
public string testCallInterfaceMethod()
{
InterfaceMethod im = new Class1();
Console.WriteLine(im.MethodA());
Console.WriteLine(im.MethodB());
Console.WriteLine(im.MethodC());
return null;
}
你们不能实例化接口,你们的代码正在编译吗?正如Adil所说:你们不能实例化接口。但您可以创建一个类,该类将只实现MethodA,并为method和methodCI引发异常。我在下面遇到错误。没有端点侦听可以接受消息。这通常是由不正确的地址或SOAP操作引起的。有关更多详细信息,请参阅InnerException(如果存在)。您提供的代码不足以解释您面临错误的原因。另外,您可以创建一个类实现接口,重写
methodA
,在方法B上保留空白体(返回null或sth),您不能实例化接口,您的代码正在编译吗?正如Adil所说:您不能实例化接口。但您可以创建一个类,该类将只实现MethodA,并为method和methodCI引发异常。我在下面遇到错误。没有端点侦听可以接受消息。这通常是由不正确的地址或SOAP操作引起的。有关更多详细信息,请参阅InnerException(如果存在)。您提供的代码不足以解释您面临错误的原因。此外,您还可以创建一个类实现接口,重写methodA
,在方法B、C上保留空白体(返回null或sth)