简体   繁体   English

一个方法可以实现多个接口方法吗?

[英]Can a method implement multiple interface methods?

These are two implementations of interface methods and as you can see, they do the same thing in this case . 这是接口方法的两种实现,如您所见, 在这种情况下 ,它们执行相同的操作。

public override IList<IDatastoreRequestParameter> GetReadByOwnerIdParameters(int id) {
    return new List<IDatastoreRequestParameter> {
        new DatastoreRequestParameter("@CustomerId", id)
    };
}

public override IList<IDatastoreRequestParameter> GetDeleteByOwnerIdParameters(int id) {
    return new List<IDatastoreRequestParameter> {
        new DatastoreRequestParameter("@CustomerId", id)
    };
}

However, as we use this interface everywhere, sometimes they are the same but they can also have different parameters returned in the implementation. 但是,由于我们在各处都使用此接口,因此有时它们是相同的,但在实现中返回的参数也可能不同。 I know I can just create a private method to return the same thing, I'm just curious to see if it's possible. 我知道我可以创建一个私有方法来返回相同的东西,我很好奇是否可行。 My gut tells me no, but SO is smarter than my gut, so here I am. 我的直觉告诉我不,但是SO比我的直觉更聪明,所以我在这里。

You cannot make one method and tell the compiler/platform to use it when calling two different methods. 您不能创建一个方法,而在调用两个不同的方法时告诉编译器/平台使用它。 As you said, you can create a private method that will handle the case and call that, but you do have to implement the methods in the interface separately. 如您所说,您可以创建一个私有方法来处理大小写并调用该方法,但是您必须在接口中单独实现这些方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM