【发布时间】:2021-06-18 13:21:56
【问题描述】:
我的问题太简单了,我有一个Interface,其中包含一个method
界面:
public interface IAbstract
{
void DoSomething();
}
我有两个类会在接口中包含相同的方法,所以我想让他们(Classes)实现接口。
问题: 唯一的问题是在其中一个类中该方法将正常实现,但在另一个类中该方法将作为泛型方法实现
我的代码想法:
头等舱:
public class ClassOne:IAbstract
{
public void DoSomething()
{
throw new System.NotImplementedException();
}
}
二等:
public class ClassTwo:IAbstract
{
public void DoSomething<int>()
{
throw new System.NotImplementedException();
}
}
我知道这给了我一个错误,但我的问题有没有什么办法可以在不创建分离接口的情况下做我认为的事情?
【问题讨论】:
-
不,你不能,因为接口
IAbstract的用户不可能知道它甚至需要一个通用参数。不清楚你要做什么,好像ClassTwo已经知道它需要是一个int为什么它需要一个通用参数?或者您可能想在ClassTwo上声明泛型参数?
标签: c# generics interface abstraction