希望你能帮助理解这一点,这样做似乎很自然:
public interface IJob { bool started { get; set; } } public interface IJobManager { void StartJob(IJob job); } public class SomeJob : IJob { public bool started { get; set; } public void OnlySomeJobDoesThis(); } public class SomeJobManager : IJobManager { public void startJob(SomeJob job) { } // --> Error'SomeJobManager' does not implement interface member 'IJobManager.StartJob(IJob)' }但正如你所看到的那样,它无法编译。 我无法想象为什么,..只要SomeJob实现IJob接口,编译器就不应该真正关心?
Hope you can help understand this, it seemed so natural to do this :
public interface IJob { bool started { get; set; } } public interface IJobManager { void StartJob(IJob job); } public class SomeJob : IJob { public bool started { get; set; } public void OnlySomeJobDoesThis(); } public class SomeJobManager : IJobManager { public void startJob(SomeJob job) { } // --> Error'SomeJobManager' does not implement interface member 'IJobManager.StartJob(IJob)' }But it won't compile, as you can see. I can't imagine why, .. as long as SomeJob implements the IJob interface the compiler should not really have to care ?
最满意答案
两个问题。
首先,C#区分大小写。 您将方法定义为StartJob (大写S),但实现startJob (小写s)。
其次,你的方法接受了实现IJob 。 但是您的界面说该方法应该能够接收IJob 。 也就是说,任何实现接口的类,不仅限于SomeJob 。
Two issues.
First, C# is case sensitive. You define the method as StartJob (capital S), but implement startJob (lowercase s).
Second, your method takes in SomeJob which implements IJob. But your interface says the method should be able to take in an IJob. That is, any class which implements the interface, which isn't limited to just SomeJob.
更多推荐
发布评论