C# 通用语言中的多个接口
我在where中使用泛型实现了多个接口。下面是我的代码C# 通用语言中的多个接口,c#,generics,interface,C#,Generics,Interface,我在where中使用泛型实现了多个接口。下面是我的代码 public class createGenericClass<T> where T : IWebService,IVoiceService { public void CallDoSomeThing(T t, int x, int y) { t.DoSomeThing(x, y); } public void CallDoSomeThingElse(T t, int a, fl
public class createGenericClass<T> where T : IWebService,IVoiceService
{
public void CallDoSomeThing(T t, int x, int y)
{
t.DoSomeThing(x, y);
}
public void CallDoSomeThingElse(T t, int a, float b)
{
t.DoSomeThingElse(a, b);
}
}
public interface IWebService
{
void DoSomeThing(int x, int y);
}
public interface IVoiceService
{
void DoSomeThingElse(int a, float b);
}
public class Web_Service : IWebService
{
public void DoSomeThing(int x, int y)
{ }
}
public class Voice_Service : IVoiceService
{
public void DoSomeThingElse(int a, float b)
{ }
}
public类createGenericClass其中T:IWebService,IVoiceService
{
公共无效调用值(T T,int x,int y)
{
t、 剂量测定法(x,y);
}
公共无效调用集合(T T、int a、浮动b)
{
t、 DoSomeThingElse(a,b);
}
}
公共接口IWebService
{
无效剂量测定(int x,int y);
}
公共接口IVoiceService
{
无效剂量(内部a,浮动b);
}
公共类Web_服务:IWebService
{
公共无效剂量测定(整数x,整数y)
{ }
}
公共类语音服务:IVoiceService
{
公共无效DoSomeThingElse(内部a,浮动b)
{ }
}
现在,我无法在main方法中创建main类的实例。我试过了,但做不到
class Program
{
static void Main(string[] args)
{
createGenericClass<IWebService> obj = new createGenericClass<IWebService>();
}
}
类程序
{
静态void Main(字符串[]参数)
{
createGenericClass obj=新建createGenericClass();
}
}
请建议。与您的设计类型
T
同时需要IWebService
和IVoiceService
。您尝试初始化obj
的方式,只使用了IWebService
,而不是IVoiceService
public class createGenericClass<T>
where T : IBaseService // telling T can be only IBaseService and inherited stuff.
{
public void CallDoSomeThing(T t, int x, int y)
{
(t as IWebService)?.DoSomeThing(x, y); // casting and validating
}
public void CallDoSomeThingElse(T t, int a, float b)
{
(t as IVoiceService)?.DoSomeThingElse(a, b); // casting and validating
}
}
public interface IBaseService{} // only gives a common parent to other interfaces. Common properties, methods can be included here.
public interface IWebService : IBaseService // inheriting the common interface
{
void DoSomeThing(int x, int y);
}
public interface IVoiceService : IBaseService // inheriting the common interface
{
void DoSomeThingElse(int a, float b);
}
一种解决方法是将泛型类拆分为多个泛型类-一个用于IWebService
,另一个用于IVoiceService
:
public class createGenericClassWeb<T> where T : IWebService
{
public void CallDoSomeThing(T t, int x, int y)
{
t.DoSomeThing(x, y);
}
}
public class createGenericClassVoice<T> where T : IVoiceService
{
public void CallDoSomeThingElse(T t, int a, float b)
{
t.DoSomeThingElse(a, b);
}
}
对于您的设计类型,
T
需要同时是IWebService
和IVoiceService
。您尝试初始化obj
的方式,只使用了IWebService
,而不是IVoiceService
public class createGenericClass<T>
where T : IBaseService // telling T can be only IBaseService and inherited stuff.
{
public void CallDoSomeThing(T t, int x, int y)
{
(t as IWebService)?.DoSomeThing(x, y); // casting and validating
}
public void CallDoSomeThingElse(T t, int a, float b)
{
(t as IVoiceService)?.DoSomeThingElse(a, b); // casting and validating
}
}
public interface IBaseService{} // only gives a common parent to other interfaces. Common properties, methods can be included here.
public interface IWebService : IBaseService // inheriting the common interface
{
void DoSomeThing(int x, int y);
}
public interface IVoiceService : IBaseService // inheriting the common interface
{
void DoSomeThingElse(int a, float b);
}
一种解决方法是将泛型类拆分为多个泛型类-一个用于IWebService
,另一个用于IVoiceService
:
public class createGenericClassWeb<T> where T : IWebService
{
public void CallDoSomeThing(T t, int x, int y)
{
t.DoSomeThing(x, y);
}
}
public class createGenericClassVoice<T> where T : IVoiceService
{
public void CallDoSomeThingElse(T t, int a, float b)
{
t.DoSomeThingElse(a, b);
}
}
解决方案1 您可以通过使用公共接口来解决此问题。该接口将由助手(
createGenericClass
)类实例使用,也将由您希望传递的其他接口使用。让我们称之为IBaseService
public class createGenericClass<T>
where T : IBaseService // telling T can be only IBaseService and inherited stuff.
{
public void CallDoSomeThing(T t, int x, int y)
{
(t as IWebService)?.DoSomeThing(x, y); // casting and validating
}
public void CallDoSomeThingElse(T t, int a, float b)
{
(t as IVoiceService)?.DoSomeThingElse(a, b); // casting and validating
}
}
public interface IBaseService{} // only gives a common parent to other interfaces. Common properties, methods can be included here.
public interface IWebService : IBaseService // inheriting the common interface
{
void DoSomeThing(int x, int y);
}
public interface IVoiceService : IBaseService // inheriting the common interface
{
void DoSomeThingElse(int a, float b);
}
解决方案3-(双重通用材料,此处不推荐)
既然是你自找的:
public class createGenericClass<T, V>
where T: IWebService
where V: IVoiceService
{
public void CallDoSomeThing(T t, int x, int y)
{
t.DoSomeThing(x, y);
}
public void CallDoSomeThingElse(V t, int a, float b)
{
t.DoSomeThingElse(a, b);
}
}
public类createGenericClass
其中T:IWebService
其中V:IVoiceService
{
公共无效调用值(T T,int x,int y)
{
t、 剂量测定法(x,y);
}
公共无效CallDoSomeThingElse(VT、int a、浮动b)
{
t、 DoSomeThingElse(a,b);
}
}
用法:
static void Main(string[] args)
{
var obj = new createGenericClass<IWebService, IVoiceService>();
obj.CallDoSomeThing(new Web_Service(), 1, 2);
obj.CallDoSomeThingElse(new Voice_Service(), 3, 4);
}
static void Main(字符串[]args)
{
var obj=new createGenericClass();
对象CallDoSomeThing(新的Web_服务(),1,2);
对象CallDoSomeThingElse(新语音服务(),3,4);
}
解决方案1
您可以通过使用公共接口来解决此问题。该接口将由助手(createGenericClass
)类实例使用,也将由您希望传递的其他接口使用。让我们称之为IBaseService
public class createGenericClass<T>
where T : IBaseService // telling T can be only IBaseService and inherited stuff.
{
public void CallDoSomeThing(T t, int x, int y)
{
(t as IWebService)?.DoSomeThing(x, y); // casting and validating
}
public void CallDoSomeThingElse(T t, int a, float b)
{
(t as IVoiceService)?.DoSomeThingElse(a, b); // casting and validating
}
}
public interface IBaseService{} // only gives a common parent to other interfaces. Common properties, methods can be included here.
public interface IWebService : IBaseService // inheriting the common interface
{
void DoSomeThing(int x, int y);
}
public interface IVoiceService : IBaseService // inheriting the common interface
{
void DoSomeThingElse(int a, float b);
}
解决方案3-(双重通用材料,此处不推荐)
既然是你自找的:
public class createGenericClass<T, V>
where T: IWebService
where V: IVoiceService
{
public void CallDoSomeThing(T t, int x, int y)
{
t.DoSomeThing(x, y);
}
public void CallDoSomeThingElse(V t, int a, float b)
{
t.DoSomeThingElse(a, b);
}
}
public类createGenericClass
其中T:IWebService
其中V:IVoiceService
{
公共无效调用值(T T,int x,int y)
{
t、 剂量测定法(x,y);
}
公共无效CallDoSomeThingElse(VT、int a、浮动b)
{
t、 DoSomeThingElse(a,b);
}
}
用法:
static void Main(string[] args)
{
var obj = new createGenericClass<IWebService, IVoiceService>();
obj.CallDoSomeThing(new Web_Service(), 1, 2);
obj.CallDoSomeThingElse(new Voice_Service(), 3, 4);
}
static void Main(字符串[]args)
{
var obj=new createGenericClass();
对象CallDoSomeThing(新的Web_服务(),1,2);
对象CallDoSomeThingElse(新语音服务(),3,4);
}
您无法使用语法创建新实例,因为您的类型“IWebService”与约束“IWebService,IVoiceService”不匹配
上面的类实现了IWebService和IVoiceService,因此它传递了约束,您将能够创建一个实例
createGenericClass<Voice_And_Web_Service> obj = new createGenericClass<Voice_And_Web_Service>();
createGenericClass obj=新建createGenericClass();
我相信其他答案非常好,涵盖了您面临的许多问题,但我没有看到一个答案准确地涵盖了您提出的关于“无法创建主类的实例”的问题。您无法使用语法创建新实例,因为您的类型“IWebService”与约束不匹配“IWebService,IVoiceService” 上面的类实现了IWebService和IVoiceService,因此它传递了约束,您将能够创建一个实例
createGenericClass<Voice_And_Web_Service> obj = new createGenericClass<Voice_And_Web_Service>();
createGenericClass obj=新建createGenericClass();
我相信其他答案都很好,涵盖了您面临的许多问题,但我没有看到一个答案完全涵盖了您提出的关于“无法创建主类实例”的问题。好的。但是有没有办法在我实现了两个接口的情况下调用单个实例/对象公共类createGenericClass其中T1:IWebService,IVoiceService“好的。但是有没有办法在我实现了两个接口的单个实例/对象中调用”公共类createGenericClass其中T1:IWebService,IVoiceService“无法“,你能告诉我们发生了什么吗?如果编译器抱怨,您需要告诉我们实际的编译器错误。“无法”,您能告诉我们发生了什么吗?如果编译器投诉,您需要告诉我们实际的编译器错误。谢谢您的回复。在您的情况下,您正在进行铸造,这对性能不好。请告诉我如何实例化这个类“public class createGenericClass where T1:IWebService,IVoiceService”。如果我有一个需要实现两个或更多接口的条件,我将如何创建实例?我已经更新了我的答案,该答案可以在不强制转换的情况下工作。如果这对你的目的不好,你不想铸造类型,那么你可能不得不使用interf