无法弄清楚为什么不允许这样做(Can't figure out why this is not allowed)

编程入门 行业动态 更新时间:2024-10-28 05:18:23
无法弄清楚为什么不允许这样做(Can't figure out why this is not allowed)

希望你能帮助理解这一点,这样做似乎很自然:

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.

更多推荐

本文发布于:2023-07-14 13:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1104803.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:这样做   不允许   弄清楚   allowed   figure

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!