Dan*_*rth 13
实现此接口是直截了当的:
public class MyInterfaceImpl : IMyInterface
{
public List<IMyInterface> GetAll(string whatever)
{
return new List<IMyInterface> { new MyInterfaceImpl(), this };
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,方法签名必须完全相同,即返回类型必须是List<IMyInterface>和不是List<MyInterfaceImpl>.
如果希望列表中的类型与实现接口的类相同,则必须使用泛型:
public interface IMyInterface<T> where T : IMyInterface<T>
{
List<T> GetAll(string whatever)
}
public class MyInterfaceImpl : IMyInterface<MyInterfaceImpl>
{
public List<MyInterfaceImpl> GetAll(string whatever)
{
return new List<MyInterfaceImpl > { new MyInterfaceImpl(), this };
}
}
Run Code Online (Sandbox Code Playgroud)
这对我有用:
public interface IMyInterface
{
List<IMyInterface> GetAll(string whatever);
}
public class Program : IMyInterface
{
public string Member { get; set; }
public List<IMyInterface> GetAll(string whatever)
{
return new List<IMyInterface>()
{ new Program() { Member = whatever } };
}
static void Main(string[] args)
{
List<IMyInterface> all = new Program().GetAll("whatever");
Console.WriteLine(all.Count);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2340 次 |
| 最近记录: |