如何用子类参数实现Interface方法? [英] How to implement Interface method with subclass argument?
问题描述
这是界面
公共界面ITransporter
{
TransporterType TransporterType {get;组;
TransportActionResults BeginTransport(
Project sourceProject,
ProjectBase targetProject);
bool CheckTargetExists(ProjectBase项目);
}
此处 ProjectBase
是所有类型的项目类的抽象类。
这是实现 ITransporter
接口的类中的方法。
public class LocalTransporter:ITransporter
{
public TransportActionResults BeginTransport(
Project sourceProject,
TargetLocalProject targetProject)
{
return TransportActionResults.Success;
}
此 TargetLocalProject
类继承自 ProjectBase
类。但是,我收到一条错误,指出LocalTransporter类没有实现接口成员并指向 BeginTransport
方法。
<这是我的问题。我需要使用基类
ProjectBase
来实现接口的 BeginTransport
方法,因为从基础继承了几个不同的项目类我们不知道会使用哪一个。 此外,有几种不同的运输者
类型所以我需要使用界面 ITransporter
与它进行交互。
注意:我考虑使用 ProjectBase
而不是 TargetLocalProject
来实现该方法,但是当使用 LocalTransporter $ c时$ c>直接(而不是thruogh接口)参数的类型应该是特定的Project类型而不是基类。
我希望自己清楚。那么,有没有办法在接口中使用基类实现这个接口方法,而在实现方法中使用子类?
你可以使用为此,只需更改您的界面:
public interface ITransporter< T>其中T:ProjectBase
{
TransporterType TransporterType {get;组; }
TransportActionResults BeginTransport(Project sourceProject,T targetProject);
bool CheckTargetExists(T project);
}
然后你可以在你的具体类中实现它:
公共类LocalTransporter:ITransporter< TargetLocalProject>
{
public TransportActionResults BeginTransport(Project sourceProject,TargetLocalProject targetProject)
{
return TransportActionResults.Success;
}
}
This is the interface
public interface ITransporter
{
TransporterType TransporterType { get; set; }
TransportActionResults BeginTransport(
Project sourceProject,
ProjectBase targetProject);
bool CheckTargetExists(ProjectBase project);
}
Here ProjectBase
is an abstract class for all types of project classes.
Here is the method inside the class that implements ITransporter
interface.
public class LocalTransporter : ITransporter
{
public TransportActionResults BeginTransport(
Project sourceProject,
TargetLocalProject targetProject)
{
return TransportActionResults.Success;
}
This TargetLocalProject
class inherits from ProjectBase
class. However, I get an error that says "LocalTransporter class does not implement interface member" and points to the BeginTransport
method.
Here is my problem. I need to implement the interface's BeginTransport
method using base class ProjectBase
because there are couple of different project classes inherited from the base class and we don't know which one will be used.
Moreover, there are couple of differen Transporter
types so that I need to interact with it using an interface ITransporter
.
Note: I considered implementing the method using ProjectBase
instead of TargetLocalProject
but when using LocalTransporter
directly (instead of thruogh interface) the argument's type should be the specific Project type instead of base class.
I hope I made myself clear. So, is there a way to implement this interface method using base class in interface but subclass in implemented method?
You can use generics for this, just change your interface a little bit:
public interface ITransporter<T> where T : ProjectBase
{
TransporterType TransporterType { get; set; }
TransportActionResults BeginTransport(Project sourceProject, T targetProject);
bool CheckTargetExists(T project);
}
Then you can implement it in your concrete class:
public class LocalTransporter : ITransporter<TargetLocalProject>
{
public TransportActionResults BeginTransport(Project sourceProject, TargetLocalProject targetProject)
{
return TransportActionResults.Success;
}
}
这篇关于如何用子类参数实现Interface方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!