简体   繁体   English

接口方法返回类型是实现接口的类

[英]Interface method return type to be class that implements the interface

I'm having trouble getting my head around interfaces. 我无法理解界面。 After trawling through similar questions on here, I've come up with the following interface, for defining the CRUD operations required for all my classes: 在这里搜索了类似的问题后,我想出了以下界面,用于定义所有类所需的CRUD操作:

public interface IData<T>
{
    IData<T> Select(int id);
    List<T> SelectMultiple();
    void Insert();
    void Update();
    void Delete();
}

This is then implemented within my partial classes: 然后在我的部分类中实现:

public partial class Post : IData<Post>
{
    public IData<Post> Select(int id)
    {
        MyDataContext dc = MyDataContext.Create();
        return dc.Posts.Single(p => p.PostID == id);
    }

    public List<Post> SelectMultiple()
    {
        MyDataContext dc = MyDataContext.Create();
        return dc.Posts.ToList();
    }

    // Update() and Delete() declarations
}

This all compiles fine, however if I try to use the Post Select() method: 这个都编译得很好,但是如果我尝试使用Post Select()方法:

Post p = new Post().Select(1);

It fails with Cannot implicitly convert type 'IData' to 'Post'. 失败, 无法将类型'IData'隐式转换为'Post'。 An explicit conversion exists (are you missing a cast?) 存在显式转换(您是否错过了演员?)

Which makes sense, but how do I have it so that it doesn't require a Cast? 这是有道理的,但我如何拥有它以便它不需要演员? I want the Select to return a Post (but not define Post as the return type at the interface level). 我希望Select返回一个帖子(但不要将Post定义为接口级别的返回类型)。 Have I misunderstood interfaces, or is there a quick alteration I can make? 我是否误解了界面,或者我可以做出快速修改?

You want to return something of type T , not IData<T> , so just change the signature (at least I guess this is what you want, as you'd return List<IData<T>> otherwise): 你想要返回T类型的东西,而不是IData<T> ,所以只需更改签名(至少我猜这是你想要的,否则你将返回List<IData<T>> ):

public interface IData<T>
{
  T Select(int id);
  List<T> SelectMultiple();
  void Insert();
  void Update();
  void Delete();
}

and implement it appropiately: 适当地实施它:

public Post Select(int id)
{
    MyDataContext dc = MyDataContext.Create();
    return dc.Posts.Single(p => p.PostID == id);
}

If you just want this behaviour in the Post class, explicitly implement the IData<T> interface: 如果您只想在Post类中使用此行为,请显式实现IData<T>接口:

public partial class Post : IData<Post>
{
  public Post Select(int id)
  {
      MyDataContext dc = MyDataContext.Create();
      return dc.Posts.Single(p => p.PostID == id);
  }

  IData<Post> IData<Post>.Select(int id)
  {
      return Select(id);
  }

}

You need to change the methods to return Post instances, then add explicit interface implementations that return the interface. 您需要更改方法以返回Post实例,然后添加返回接口的显式接口实现。

For example: 例如:

public partial class Post : IData<Post> {
    Post Select(int id) { ... }
    IData<Post> IData<Post>.Select(int id) { return Select(id); }
}

What about trying 尝试怎么样?

public Post Select(int id)
{
    MyDataContext dc = MyDataContext.Create();
    return dc.Posts.Single(p => p.PostID == id);
}

ie, return directly Post instead of IData<Post> . 即,直接返回Post而不是IData<Post>

You should explicitly implement IData<Post>.Select and provide your own Select with the appropriate return value. 您应该显式实现IData<Post>.Select Select并使用适当的返回值提供您自己的Select For example: 例如:

IData<Post> IData<Post>.Select(int id)
{
    return Select(id);
}

Post Select(int id)
{
    MyDataContext dc = MyDataContext.Create();
    return dc.Posts.Single(p => p.PostID == id);
}

However, if you do this: 但是,如果你这样做:

IData<Post> post = new Post();
Post p = post.Select(1);

post.Select(1) still returns IData<Post> . post.Select(1)仍返回IData<Post> See Femaref's answer on a refactoring of the interface to allow this. 请参阅Femaref关于重构接口以允许此操作的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在实现通用接口方法的方法中专门化类型 - Specialize type in method that implements a generic interface method 如何从方法返回实现接口的对象 - How to return an object that implements an interface from a method 如何判断类是否实现了泛型类型的接口 - How to Tell if a Class implements an interface with a generic type 泛型类型参数是实现接口的类 - Generic type parameter is an class that implements an interface 如何禁用类型转换从接口到实现接口的类 - How to disable type casting from interface to class that implements interface 通过接口方法实现接口的类的汇编版本 - Getting Assembly Version of a class that implements interface through interface method 接口方法的签名包括一个class,它实现了接口本身 - Signature of interface's method includes a class, which implements the interface itself 除非T实现接口I,否则需要通用方法返回类型T,在这种情况下,返回I - Need generic method to return type T unless T implements interface I, in which case return I 用返回类型实现接口,该接口实现所需的返回类型 - Implementing an interface with a return type that implements the required return type 如何设计一种可以查询数据库并返回实现相同接口的不同类型的方法? - How do design a method that can query the DB and return different type that implements the same interface?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM