[英]Can derived C# interface definitions 'override' function definitions?
我正在定義一個通用API,它將具有許多更特定的派生功能,並且想知道C#接口是否足夠強大以進行建模,如果可以的話,以及如何進行建模。
為了說明我正在嘗試做的事情,請想象一個具有通用接口的身份驗證API,該接口帶有一個Authenticate函數,該函數需要一個抽象的AuthenticationToken。 然后,我想用如下所示創建此接口的更特定形式。
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)
{
}
}
我的意圖是,派生的接口必須遵守高級格式,但這不是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)
{
}
}
有約束的泛型!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.