我正在定义一个通用 API,它将有许多更具体的派生,并且想知道 C# 接口(interface)是否足够强大来对此建模,如果是,如何建模,如果不是,我还可以如何建模。
为了说明我正在尝试做的事情,请想象一个具有通用接口(interface)的身份验证 API,该接口(interface)具有一个采用抽象 AuthenticationToken 的 Authenticate 函数。然后我想创建此界面的更具体形式,如图所示..
abstract class AuthenticationToken
{
}
interface IAthentication
{
bool Authenticate(string name, AuthenticationToken token);
}
class DoorKey : AuthenticationToken
{
}
interface IDoorAthentication : IAthentication
{
bool Authenticate(string name, DoorKey token);
}
class DoorUnlocker : IDoorAthentication
{
public bool Authenticate(string name, DoorKey token)
{
}
}
我的意图是派生接口(interface)被限制为符合高级形式,但这不是 C# 解释它的方式。
最好的问候
帮助我 John Skeets .. 你是我唯一的希望。 (抱歉......我的星球大战蓝光已经到了!)
最佳答案
这就是你想要的:
abstract class AuthenticationToken
{
}
interface IAthentication<T> where T : AuthenticationToken
{
bool Authenticate(string name, T token);
}
class DoorKey : AuthenticationToken
{
}
interface IDoorAthentication : IAthentication<DoorKey>
{
}
class DoorUnlocker : IDoorAthentication
{
public bool Authenticate(string name, DoorKey token)
{
}
}
有约束的泛型!
关于c# - 能否导出C#接口(interface)定义 'override'函数定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7792244/